Routing parameters in frameworks

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
User avatar
kr1pt
Forum Newbie
Posts: 8
Joined: Sun Jan 30, 2011 4:38 am
Location: Croatia

Routing parameters in frameworks

Post 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 :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Routing parameters in frameworks

Post by Weirdan »

ZF does all the parsing on the PHP side, same could be true for the CI as well.
User avatar
kr1pt
Forum Newbie
Posts: 8
Joined: Sun Jan 30, 2011 4:38 am
Location: Croatia

Re: Routing parameters in frameworks

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Routing parameters in frameworks

Post 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.
User avatar
kr1pt
Forum Newbie
Posts: 8
Joined: Sun Jan 30, 2011 4:38 am
Location: Croatia

Re: Routing parameters in frameworks

Post 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...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Routing parameters in frameworks

Post 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
Goldwinterrain
Forum Newbie
Posts: 4
Joined: Sun Mar 13, 2011 9:15 pm

Re: Routing parameters in frameworks

Post 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 :)
Blackshawk
Forum Newbie
Posts: 5
Joined: Thu Feb 10, 2011 8:16 am

Re: Routing parameters in frameworks

Post 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).
Post Reply