V
V
Vladimir Borisyuk2018-04-26 08:09:13
PHP
Vladimir Borisyuk, 2018-04-26 08:09:13

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

1 answer(s)
D
Dmitry, 2018-04-26
@By_Engine

I tried 2 and 3. If there is no route in the list - automatically detect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question