Good day, community! I want to write for myself a good router, I looked at different options: with routes, without routes, control only through URI, etc. and etc. I have a little PHP programming experience, so after reading here, there I decided to write here. Solve this question, so to speak, in living answers. The bottom line: I want my routes to be like this

return array( "index"=>array("controller"=>"index", "method"=>"index","args"=>array("number"=>"2", "type"=>"family")), "notes/([-_a-z0-9]+)"=>array("controller"=>"index", "method"=>"index", "args"=>array("param"=>"2", "mouse"=>"grey")), "note/([/d])"=>array("controller"=>"index", "method"=>"index", "args"=>array("number"=>"2", "type"=>"family")), ); 

In other words, I want to take a string from the URL, and, comparing it with the routes, get the controller, method, arguments. (As always shorter).

By the principle of this:

 return array( 'about' => 'page/show/about', 'page/([-_a-z0-9]+)' => 'page/show/$1', 'users/([-_a-z0-9]+)' => 'users/show/$1', ); 

Now questions:

  1. I do not quite understand the principle of key matching, which uses a regular expression with a value. Namely this:

     'page/([-_a-z0-9]+)' => 'page/show/$1' 
    Did I understand correctly that the regular expression ([-_a-z0-9] +) and $ 1 are the same? How is it read? Like, $ 1 is assigned the value of a regular expression in an array key?

  2. Do you think that such a detailed splitting of the route into controller-method-arguments in the route array itself will in some way make it difficult to work in the future? Is it worth doing this? Maybe it's better to receive a string as usual, and only then, in the function itself, break it and continue to work with it?

Yes, I looked at different implementations of routers, in frameworks and just solutions. I would not want the extra manipulations that are usually offered in frameworks. As for bicycles: I am not going to invent something new, I just want to make a convenient solution for me to work and so that I fully understand what is being done there. Probably a trashed topic, but I think everyone has some preferences in finding some kind of solution. Maybe your decision will be the point of this question.

Thanks for answers.

  • one
    In addition, I will note that nothing is supplemented by myself, as I could by my inexperience think. Need to use preg_replace (); - shmihshmih

1 answer 1

  1. Yes you are right. The construction ([-_a-z0-9]+) is regular. And $ 1 is assigned the result of this regular season.
  2. In your scheme, I see one problem. There is no division into methods (GET, POST). Sometimes it is useful, depending on the method, to call a specific function, convenient for building APIs. And so I would have bothered to write in such detail. Here it is a matter of convenience.

I am more accustomed to this method.

 $router->get('/blog/(\/[A-Za-z0-9\.,_-]+)', function($post_id){ return (new MainСontroller())->getIndex($post_id); }); 

It’s convenient here that the IDE has some tips.