views:

220

answers:

2

I'm currently working on my own PHP Framework, and I need some help figuring out if I'm going in the right direction or not...

The framework is both for my own use and to generally advance my PHP skills further. I've encountered numerous problems that by overcoming them I have learned a great deal, and love being able to create something from nothing, so I'd rather not see answers like "Just use Zend"! ;)

I have read a bunch of articles both on Stack Overflow and a bunch of other sites, but can't quite get the right answer I need, so hopefully someone can give me some helpful advice!

I've tried a few different solutions, but I've just ended up confusing myself and I'm not sure which direction to go now! Can't quite get my head around it all...

'Theoretical' framework structure

- .htaccess
- index.php
- private/
    - app/
        - bootstrap.php
        - modules/
            - default/
                - controllers/
                    - pages.php
                    - index.php
                - models/
                - views/
            - admin/
                - controllers/
                - models/
                - views/
    - config/
        - config.php
        - autoloader.php
    - lib/
        - Some_Library
            - Class1
                - class1.php
            - Class2
                - class2.php
- public/
    - css
    - images
    - scripts

Details

  • index.php is the main file, where every request is routed to with .htaccess.
  • private/ can't be accessed publicly, obviously.
  • public/ contains all the public files.
  • app/ contains all app-specific code.
  • lib/ could contain Zend or another library (I'm also working on my own), to be called with autoloaders
  • bootstrap.php is the app-specific code... Do I need this? is the main 'index.php' enough?.
  • modules/ would contain each module... Do I need modules at all?.
  • default/ is the default module that will contain the MVC's for most requests (used when 'admin' isn't the first part of the URL).
  • admin/ is the module that will contain the MVC's for the admin section.

Anyways, to my question...

I thought it would be better to separate the admin section from the rest of the website, but that's where I'm getting stuck. I have made the above structure to work with it, but I'm not sure if this is the most effective way.

If a request site.com/videos/view/1/ comes to my site..

Module: Default Controller: Videos Action: View Params: array( '1' )

and if the request site.com/admin/pages/view/1/ comes to my site..

Module: Admin Controller: Pages Action: View Params: array( '1' )

Is this the right way to go about this? Or am I over-complicating it and doing something that's not worth doing?

Should I have a completely separate application framework for my admin section...? Do I even need to separate the admin section's MVC's from the rest of it all?

Sorry for the massive question, just wanted to give you as much info as possible! Feel free to answer whichever part you can =P

+1  A: 

i would suggest you to use a bootstrap.php that manages all the routings so you never run into issues like "i wish i could nest one folder more into my admin module".

i also wouldnt use modules and keep the default controllers right inside the controller/ dir and the admin controllers inside the controller/admin dir. same for models and views.

btw its really not clever not to share the models between different parts of your application, they are going to be the same in 99% of all cases. thats why mvc is so powerful. sometimes you even can share some of the view parts inside your app between the front- and backend.

antpaw
I have created a Router class, which will accept a 'reroute' variable/array of some sort, probably in the form of a string. The variable will be something like "admin", and if that pattern gets matched in the URL, the Router will configure itself to load controllers from the 'admin/' folder. I will think of more efficient/effective ways after I get things sorted out..Modules have been scrapped, I see why they're not needed =)And I will now be sharing models and some views, I can see how that makes more sense now!Thanks!
manbeardpig
+1  A: 

One Solution for admin routing is what CakePHP does, you first define a configuration for the admin string and then in your controller use actions with a specific naming convertion

//Configuration ============================
Configure::write("admin_routing" , true );
Configure::write("admin_prefix"  , "admin" );

//Controller ===============================
class MyController extends AppController{

    function index(){
      //Will map to /mycontroller/
    }


    function admin_index(){
      //Will map to /admin/mycontroller/
    }

}

You can generalize this by using a routing system just look how your favorite framework does it

On another note

  1. The modules folder seems to be unecesary
  2. I agree with antpaw you should add a globals view and model folder in order to share them across applications
  3. I don't get why autoloader is inside the config directory and not as part of the lib directory, you can also just move the boostrap.php to the config directory

Hope this helps

Mon Villalon
I have incorporated the CakePHP solution you showed above into my current code, with a few changes. Instead of having the actions for both default and 'admin' requests in the same Controller, I have made it so there is an "Index_Controller" located in "controllers/indexController.php", as well as "Admin_Index_Controller" in "controllers/admin/indexController.php". I think this solution will work, unless you can see something wrong with it..? Regarding #2: Do you mean I should add a 'views' and 'models' folder outside the app dir? Or just add a 'views/common' dir and 'models/common'?
manbeardpig
Quick question.. does that MyController class call other controllers?? If my Routing class (see my comment to antpaw's answer) can configure which controller/action to run, would that MyController class be necessary? I don't quite see why you initiate a controller -just- to initiate other controllers.. But maybe I just don't know enough about MVC or Cake..
manbeardpig
1. The Admin_Index_Controller seems like a reasonable solution. It has the benefit of separating admin an regular logic in two diffent classes.2. I don't exactly get what you mean by call other controllers, its just "the controller", the routing system looks at the url and maps it to this class. The admin_index function is just the code behind a regular action, is just routed a little bit different. Instead of/mycontroller/admin_index/ it would map to /admin/mycontroller/index/ because of the configuration
Mon Villalon
I think I got confused where it said "this will map to..." thinking that those actions will load controllers from those folders, but i think it just means that those actions are called by those urls, not that they call them..right? I think i want to have admin controllers etc in a completely diff class and structure to the normal ones. users/add routes (via a router in the bootstrap) to controllers/usersController.php users_controller->add() and admin/users/add routes to controllers/admin/usersController.php (or admin/controllers/usersControllers.php??) admin_users_controller->add(). Thoughts?
manbeardpig
Sorry, ignore the "s" on the link in the brackets!
manbeardpig
Regarding the Autoloader - If I put it in my Core_Autoloader class in /private/lib/Core/Autoloader.php, and I want to initiate it from my index.php file with 'spl_autoload_register(array('Core_Autoloader','autoload'));', I would have to manually include the class file...which kind of defeats the purpose of an autoloader, right?
manbeardpig