Answer the question
In order to leave comments, you need to log in
What is the best way to make a Router in an MVC program?
I made my own mvc framework, but I don't know how best to organize Router.
1) Use $_GET parameters like /site/?controller=news&action=index
2) Split URL into segments:
$routes = explode('/', $_SERVER['REQUEST_URI']);
if ( !empty($routes[2]) ) {
$controllerName = $routes[2]; //Controller name
}
if ( !empty($routes[3]) ) {
$actionName = $routes[3]; // Action name
}
3) Write routes as an array:
$routes = array(
'news' => 'news/index',
'news/([0-9])' => 'news/view'
)
Then loop use regular expressions to look for matches in $_SERVER['REQUEST_URI'],
In cases 1 and 2, controller and action are determined automatically.
B 3 It is necessary to prescribe routes in advance, as in laravel Route::get('/', '[email protected]');
Which way is more flexible and promising?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question