Page 1 of 1

Routing parameters in frameworks

Posted: Sun Jan 30, 2011 5:04 am
by kr1pt
In which way CodeIgniter, cakePHP or Zend Framework are routing HTTP requests.

Is it like this:

Code: Select all

Blog index:
http://localhost/Blog/ or http://localhost/Blog/index

New post:
http://localhost/Blog/new_post

Code: Select all

class Blog // implements Controller; extends Controller
{
    public function index()
    {
        $this->get_view('blog_index');
    }
    
    public function new_post()
    {
        $this->get_view('blog_new_post');
    }
}
This is the way frameworks control methods as subpages, but how 'bout parameters. It is easy when method in controller has only one parameter.
Example:

Code: Select all

public function get_post($id = null)
    {
        if (!is_null($id))
        {
            // querying post
        }
    }
Then URL will be:

Code: Select all

Getting searched post:
http://localhost/Blog/get_post/id (http://localhost/Blog/get_post/1)
But what if method has two, three or four parameters, how to automaticly generate path seperators with parameters through .htaccess?

In $_GET, that would be like this:

Code: Select all

http://localhost/index.php?controller=Blog&method=get_post&parameter1=id
But what if there are more parameters, that should be:

Code: Select all

http://localhost/Blog/get_post/id/name/something_else
or (without .htaccess)

Code: Select all

http://localhost/index.php?controller=Blog&method=get_post&parameter1=id&parameter2=name&parameter3=something_else
This is the way CodeIgniter controls routes, but I can't find specific code for generating .htaccess, or $_GET.

I hope you understand what I mean, so please help :)

Re: Routing parameters in frameworks

Posted: Sun Jan 30, 2011 9:34 am
by Weirdan
ZF does all the parsing on the PHP side, same could be true for the CI as well.

Re: Routing parameters in frameworks

Posted: Sun Jan 30, 2011 9:43 am
by kr1pt
Weirdan wrote:ZF does all the parsing on the PHP side, same could be true for the CI as well.
You mean they don't use htacces? But how to control paths with PHP without htaccess file and rewrite module?
$_SERVER, pathinfo(), basename() or what?

Re: Routing parameters in frameworks

Posted: Sun Jan 30, 2011 9:47 am
by Weirdan
They do use .htaccess, but only to route any request to non-existing file to index.php. After that frameworks takes care of parsing $_SERVER['REQUEST_URI'] and mapping it to appropriate controller/action.

Re: Routing parameters in frameworks

Posted: Sun Jan 30, 2011 9:52 am
by kr1pt
I analyzed CI, but I don't understand how they map when user has URL let's say like this:

Code: Select all

http://localhost/something/framework/

Code: Select all

$url = $_SERVER['REQUEST_URI'];
$array_of_urls = explode('/', $url);
With this code, script should start looking for controllers and method in $array_of_urls[3], 'cuz that's where framework path starts. I tried to search how to "kill" URLs before location where framework is stored, because other users have different URL's, with more/less paths, so everytime there will be different $array_of_urls, but no result in searching...

Re: Routing parameters in frameworks

Posted: Sun Jan 30, 2011 12:40 pm
by Weirdan
They use the path to entry point (index.php) script to detect constant part of the path. Most of the dirty work with urls seems to be done in URI class: https://bitbucket.org/ellislab/codeigni ... re/URI.php

Re: Routing parameters in frameworks

Posted: Sun Mar 13, 2011 10:03 pm
by Goldwinterrain
But what if method has two, three or four parameters, how to automaticly generate path seperators with parameters through .htaccess?

In $_GET, that would be like this:

Code: Select all
http://localhost/index.php?controller=B ... ameter1=id



But what if there are more parameters, that should be:

Code: Select all
http://localhost/Blog/get_post/id/name/something_else


or (without .htaccess)

Code: Select all
http://localhost/index.php?controller=B ... thing_else



This is the way CodeIgniter controls routes, but I can't find specific code for generating .htaccess, or $_GET.

I hope you understand what I mean, so please help :)

Re: Routing parameters in frameworks

Posted: Fri Mar 18, 2011 10:59 am
by Blackshawk
In most MVC applications you set up routing rules in PHP that associate positions within the URI with a key name. For example:

/home/user/123432/Johnny-Appleseed

This is taken as a string and broken up into an array which is fed into the routing rules and you get:

controller = home
action = user
userid = 123432
name = Johnny Appleseed

This is handled differently in various frameworks - some easier, others more difficult. It really depends on how much fine-grain control you want over your routing. As someone stated earlier, the htaccess file is really for rewriting requests through your index.php page (although it certainly can do other things as well).