Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    MVC Files/Folder Structuring

    Hi,

    I've been trying to get my head around MVC frameworks recently, since I'm starting my first Web Dev job in a few weeks and I've not really understood them up till now.

    I understand the basics of them, but there are a few things that are confusing me. One of them is folder structure.

    Now I know that different frameworks and different examples have different structures, so I'm really just looking for a simple example of how this could be all set out.

    Up till now I've been playing around with some "design your own simple framework" tutorials, and I ended up with something along the lines of:

    application/
    control/
    IndexController.php
    ItemController.php
    etc...
    library/
    model/
    Item.class.php
    etc...
    view/
    IndexTemplate.php
    ItemTemplate.php
    etc...
    Index.php
    etc...


    Which works as such, with the following example URL:

    mysite.com/item/view/1

    - Loads Index.php page. This sorts out what controller to call.
    - Loads ItemController.php - Which in turn, loads ItemTemplate.php Template
    - Calls view() method of ItemController with the parameter of "1" - view(1)
    - That method access the Item model and does various stuff
    - Any template methods are called, eg. $Template->view() to display the view HTML
    - Template is rendered, using $Template->render() method


    Now, I know that's probably a poor way of doing it, but it works for the simple-ish application I've been making.

    Anywho, I've come to the point now where I realised that I'm going to be needing quite longer URLs for another project sometimes, but I'm not sure how to go about implenting it.

    For example, one URL which might be access could be something like:

    mysite.com/user/view/9

    Where I would want the User Controller to be loaded, the view() method to be called with "9" as the parameter.

    But other times the URL might be something like:

    mysite.com/guide/activities/hiking/locations/river1

    And I can't see any way of doing that without calling an activities() method and then doing various switch statements until it gets to the required content. Which seems like it defeats the point of making it easier to use and develop with.

    I've seen one or two example with lots of sub folders, but I'm yet to find any actually good and informative documentation or examples on any of this, so it's all guess work.

    If you were going to have longer URLs like that, what would you suggest as to how to structure the files? And how do you get around the issue that sometimes you will be wanting a /controller/action/parameter whereas others you'll just be wanting /controller/content/subcontent/subcontent?

    Basically: I'm confused and I can't find any decent explanations for any of this.

    Thanks.

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    It's usually not a good idea to hardcode the URL routing into the framework. All the MVC frameworks I've worked with yet implement some sort of routing component that allow you to define what's what in a URL (for instance using regexps) and call the appropriate method of the appropriate controller, passing on the appropriate parameters accordingly.

    For instance, if the URL "mysite.com/guide/activities/hiking/locations/river1" means that there is an "activity" controller that handles all activity requests, and you have "activity" and "location" models with a one-to-many relationship, and the "guide" information in the URL is useless for the inner workings of the backend, you will want to route that request to, say, the view_location method of the activity controller, and pass it the activity name (hiking) and location name (river1), which it in turn may pass to to the constructors of your models to get you the appropriate data.

    Basically, you don't want to have the structure of an URL dictate your folder structure (or file/class names, for that matter).

    Personally, I enjoy working with the Kohana framework — its sources are rather readable, and it might be helpful for you to have a read through how routing is done there (http://kohanaframework.org/3.0/guide/kohana/routing) and look through the source to get an idea of the actual implementation.

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by venegal View Post
    It's usually not a good idea to hardcode the URL routing into the framework. All the MVC frameworks I've worked with yet implement some sort of routing component that allow you to define what's what in a URL (for instance using regexps) and call the appropriate method of the appropriate controller, passing on the appropriate parameters accordingly.

    For instance, if the URL "mysite.com/guide/activities/hiking/locations/river1" means that there is an "activity" controller that handles all activity requests, and you have "activity" and "location" models with a one-to-many relationship, and the "guide" information in the URL is useless for the inner workings of the backend, you will want to route that request to, say, the view_location method of the activity controller, and pass it the activity name (hiking) and location name (river1), which it in turn may pass to to the constructors of your models to get you the appropriate data.

    Basically, you don't want to have the structure of an URL dictate your folder structure (or file/class names, for that matter).

    Personally, I enjoy working with the Kohana framework — its sources are rather readable, and it might be helpful for you to have a read through how routing is done there (http://kohanaframework.org/3.0/guide/kohana/routing) and look through the source to get an idea of the actual implementation.
    Let's say for example sake a website has these 3 possible urls:

    /index
    /activities/hiking/locations
    /user/view/1

    What I don't understand is how you can route them all to the correct places, without hard coding it all in.

    For example:

    /index - You would just want the default indexcontroller to be called. Nothing else. That's fine, I can do that.

    /activities/hiking/locations - You could do as you mentioned as call activitiescontroller, then call locations(hiking).

    But what if you then had the /user/view/1

    Where you wanted the usercontroller, then the view(1) method. That's the reverse of the previous example, as that did:

    /ctrl/param/method - controller->method(param)

    Whereas this user/view/1 would be /ctrl/method/param

    How is the router supposed to know what to do, so that it doesn't, for example try to call 1(view) or hiking(locations).


  • #4
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by aaaa View Post
    What I don't understand is how you can route them all to the correct places, without hard coding it all in.
    You have to define your routes, of course. This can not be considered hard coding, though, as those route definitions are not part of the framework itself, but part of your application configuration (obviously, you will have different route definitions for different applications based on the same framework).

    Using regexps in your routing component ensures that you can set up a rather complex routing system with just a few routing definitions in your config file — see the link I provided in my previous post for examples from an actual framework.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •