Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage URL routes in own php framework

I'm creating a PHP Framework and I have some doubts...

The framework takes the url in this way: http:/web.com/site/index

It takes the first parameter to load controller (site) and then loads the specific action (index).

If you've installed the framework in a base URL works ok, but if you install it in a subfolder like this: http://web.com/mysubfolder/controller/action

My script parses it as controller = mysubfolder and action = controller.

If you have more subfolders the results will be worst.

This is my Route code:

Class Route
{
    private $_htaccess = TRUE;
    private $_suffix = ".jsp";

    public function params()
    {
        $url='';

        //nombre del directorio actual del script ejecutandose.
        //basename(dirname($_SERVER['SCRIPT_FILENAME']));

        if($this->_htaccess !== FALSE):
            //no está funcionando bien si está en un subdirectorio web, por ej stynat.dyndns.org/subdir/
            // muestra el "subdir" como primer parámetro
            $url = $_SERVER['REQUEST_URI'];
            if(isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])):
                $url = str_replace("?" . $_SERVER['QUERY_STRING'], '',$url);
            endif;
        else:
            if(isset($_SERVER['PATH_INFO'])):
                $url = $_SERVER['PATH_INFO'];
            endif;
        endif;

        $url = explode('/',preg_replace('/^(\/)/','',$url));
        var_dump($url);

        var_dump($_GET);

    }
}

Thanks for any help you can give.

like image 532
Stefan Luv Avatar asked Aug 02 '12 23:08

Stefan Luv


People also ask

How do I route a URL?

The handler can be a physical file, such as an . aspx file in a Web Forms application. A handler can also be a class that processes the request. To define a route, you create an instance of the Route class by specifying the URL pattern, the handler, and optionally a name for the route.

How do php routes work?

In its most common configuration, PHP relies on the web server to do the routing. This is done by mapping the request path to a file: If you request www.example.org/test.php, the web server will actually look for a file named test. php in a pre-defined directory.

What is routing in Yii Framework?

When a Yii application starts processing a requested URL, the first step it takes is to parse the URL into a route. The route is then used to instantiate the corresponding controller action to handle the request. This whole process is called routing.


1 Answers

You are missing a base path. The routing script must now where to start when detecting a pattern or route detected.

Pseudo code:

 //set the base URI
$base_uri = '/base';
//remove the base URI from the original uri. You can also REGEX or string match against it if you want
$route_uri = str_replace($base_uri,'',$uri);
//perform route matching $route_uri, in your code a simple explode

$url = explode('/',preg_replace('/^(\/)/','',$route_uri));

You can use this with or without RewriteBase for your .htaccess so long as they use the same harness - index.php.

Additionally, you can improve your route match procedure using Regular Expressions function like preg_match and preg_match_all. They let you define a pattern to match against and results to an array of matching strings - see http://php.net/manual/en/function.preg-match.php.

like image 190
Rum Verse Avatar answered Sep 30 '22 00:09

Rum Verse