How to build a good router for php mvc

I am experimenting with php mvc and I had the following problem. My request and router classes are very simple, and I would like to expand the topic to handle controller calls from subfolders and to controller classes, functions should be able to pick up URLs, send them to receive and send.

my router looks like it should

class Router{

    public static function route(Request $request){


        $controller = $request->getController().'Controller';

        $method = $request->getMethod();

        $args = $request->getArgs();


        $controllerFile = __SITE_PATH.'/controllers/'.$controller.'.php';


        if(is_readable($controllerFile)){
            require_once $controllerFile;

            $controller = new $controller;


            if(!empty($args)){
                call_user_func_array(array($controller,$method),$args);
            }else{  
                call_user_func(array($controller,$method));
            }   
            return;
        }

        throw new Exception('404 - '.$request->getController().'--Controller not found');
    }
}

and request class

    private $_controller;


    private $_method;

    private $_args;

    public function __construct(){

        $parts = explode('/',$_SERVER['REQUEST_URI']);


        $this->_controller = ($c = array_shift($parts))? $c: 'index';
        $this->_method = ($c = array_shift($parts))? $c: 'index';

        $this->_args = (isset($parts[0])) ? $parts : array();

    }

    public function getController(){

        return $this->_controller;

    }
    public function getMethod(){

        return $this->_method;

    }
    public function getArgs(){

        return $this->_args;
    }
}

The problem is this: when I try to send thrown ajax, the variables to the controller method are not recognized due to the structure of the URL. for instance

index/ajax?mod_title=shop+marks&domain=example

accepted only if it looks

index/ajax/shop+mark/example
+5
source share
4 answers

LFI .
, $controller, -, NUL-, , , , , , /etc/passwd, config , .

; !

edit:

$safe = array(
    'ajax',
    'somecontroller',
    'foo',
    'bar',
);
if(!in_array($this->_controller, $safe))
{
    throw new Exception(); // replace me with your own error 404 stuff
}
+10

Request URI , , , $_GET $_REQUEST, .

, . :

:

$this->_args = (isset($parts[0])) ? $parts : array();

:

$all_parts = (isset($parts[0])) ? $parts : array();
$all_parts['get'] = $_GET;
$this->_args = $all_parts;

, $_GET (.. , url) , $args ( $args ['get'] , , $_GET, domain = example, $args ['get'] ['domain']).

, (, ), :

public function query($var = null)
{
    if ($var === null)
    {
        return $_GET;
    }
    if ( ! isset($_GET[$var]) )
    {
        return FALSE;
    }
    return $_GET[$var];
}

, url (, $request- > ('domain')) $_GET ($ request- > query()).

+2

, php "? mod_title=..." $_GET. getArgs() $_GET, $_POST $_REQUEST.

MVC, rasmus: http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html

If your use case will be more complex, look like Zend (http://framework.zend.com/manual/en/zend.controller.html) or Symfony (https: // github.com/ symfony / symfony / tree / master / src / Symfony / Component / Routing) do their stuff.

0
source

Choose any popular MVC to see how they implement it under the hood. In addition, spl_autoload_register and namespace are your friends.

0
source

All Articles