Jump to content

Recommended Posts

hey guys im trying to re-write my framework routing system...which to be honest is a mess.

 

after a bit of reading i see people are using regular expression to match uri's...now what i've done is simplistic at the moment...but im wondering if i'm on the right track and if you guys could give me some useful suggestions please.

 

here is my code i've started

<?php

class Router
{
    protected static $_routes = array();
    
    public static function add_route($method, $uri, $route)
    {
        self::$_routes[] = array('method' => $method,
                                 'uri'    => $uri,
                                 'route'  => $route
        );
    }
    
    public function get_routes()
    {
        return self::$_routes;
    }
    
    public function is_route()
    {
        $routes  = $this->get_routes();
        $method  = $_SERVER['REQUEST_METHOD'];
        $request = '/login'; //$_SERVER['REQUEST_URI'];
        
        foreach ($routes as $route)
        {
            $uri     = $route['uri'];
            $uri     = str_replace('/', '\/', $uri);
            $pattern = '/^\/' . $uri . '$/i';
            
            if ($method === $route['method'] &&
               preg_match($pattern, '/login', $matches))
            {
                return true;
            }
        }
        
        return false;
    }
}

Router::add_route('GET', 'login', array('contoller' => 'foo',
                                        'action'    => 'bar'
));

$router = new Router;

if ($router->is_route())
{
    echo "successful route";
}
else
{
    echo "page error";
}

what i'd like to know is how i would go about putting things like this into my url

 

variables                      -  search/:item   so i can retrieve :item value

set variable                  - news/:action(add|edit|delete)   matching /news/ with add, edit or delete at the end

and something like this - items[/test]    allowing /items or /items/test to work for a certain route

 

some advice on this would be greatly appreciated...thanks guys

 

 

Link to comment
https://forums.phpfreaks.com/topic/296936-mvc-router/
Share on other sites

Did you have a look at what can return with $_SERVER to determine the uri?

 

As for the variables can do them with GET ... ?action=edit

In this case would check if $_GET['action'] is set and not empty or within an array that you allow.

For the multiple values can do a switch or if/else.

 

I see what you are trying to do, for my one cms I made a controller that handles what to include.

Everything goes through index page

I include the controller script

Controller script determines which script to load depending uri or parameters, also my allowed GET array

I use a switch to determine which script to load (you could do similar get values and script names and append the extension)

If file exists include that script

Link to comment
https://forums.phpfreaks.com/topic/296936-mvc-router/#findComment-1514485
Share on other sites

yeah everything goes through my index too and i use this in my .htaccess

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri=$1 [PT,L,QSA]

so i can get the uri from $_GET['uri'] or $_SERVER['REQUEST_URI'];...

 

but have no idea the best way to extract a variable from a route such as items/:item    - items/shoes

and to return shoes...

Link to comment
https://forums.phpfreaks.com/topic/296936-mvc-router/#findComment-1514499
Share on other sites

i come up with the idea of this for getting uri variables...am i just do things the hard way?

<?php

class Router
{
    protected static $_routes = array();
    
    public static function add_route($method, $uri, $route)
    {
        self::$_routes[] = array('method' => $method,
                                 'uri'    => $uri,
                                 'route'  => $route
        );
    }
    
    public function get_routes()
    {
        return self::$_routes;
    }
    
    public function is_route()
    {
        $routes  = $this->get_routes();
        $method  = $_SERVER['REQUEST_METHOD'];
        $request = '/login/foo/bar'; //$_SERVER['REQUEST_URI'];
        
        foreach ($routes as $route)
        {
            $uri = $route['uri'];
            $uri = str_replace('/', '\/', $uri);
            $uri = '/^\/' . $uri . '$/i';
            
            
            // check for :variables
            if (preg_match_all('/(?<=\w+/', $uri, $parameters))
            {
                $parameters = $parameters[0];
                
                // replace :variables
                $uri = preg_replace('/:(?<=\w+/', '(.*?)', $uri);
                
            }
            
            if ($method === $route['method'] &&
               preg_match($uri, $request, $variables))
            {
                unset($variables[0]);
             
                if (!empty($parameters) &&
                    !empty($variables))
                {
                    $parameters = array_combine($parameters, $variables);
                    print_r($parameters);
                }

                return true;
            }
        }
        
        return false;
    }
}

Router::add_route('GET', 'login/:test1/:test2', array('contoller' => 'foo',
                                                      'action'    => 'bar'
));

$router = new Router;

if ($router->is_route())
{
    echo "successful route";
}
else
{
    echo "page error";
}
Link to comment
https://forums.phpfreaks.com/topic/296936-mvc-router/#findComment-1514500
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.