Answer the question
In order to leave comments, you need to log in
Good example of routing/cnc implementation?
I will not go into the history of the project. I will say that now the stage is approaching when programmers will have time to partially rewrite the old and terrible code. I consider the CNC system to be a priority. It works like this:
- CNC class, does explode URL by slashes (then a crutch appeared, equating "_" to "/").
— The first element of the array is the name of the module. A huge switch that finds and transfers control to the module.
- A special module method, is a minichpu, according to the same principle. It parses the second parameter and passes control to the desired method.
The system basically works, it used to be even worse and even the necessary module method in the main CNC class was called, as a result of which, it was often that some functions disappeared when rewritten. But the problem is not even in this, but in the fact that there are no conveniences with the parameters in the URL. If, for example, /news/1740/page/2
Then you have to focus on the order of the parameters, which is not good if some, for example, will not participate.
I ask for advice and a righteous kick.
Frameworks with a good and flexible routing system, I recently saw Kohana, got interested. Most likely we will not fasten the whole framework. Your experience, advice, maybe nuances that are better to look at are interesting. Examples from other frameworks, now there are many good ones, but there is no time to study each one.
PHP project.
Thank you for your attention and time spent on me.
I hope I asked the question and described the problem clearly. There will be clarifications - forward. I'll give you pluses :)
Answer the question
In order to leave comments, you need to log in
symfony.com/doc/current/book/routing.html - check it out, you might be curious.
In any case, making a rigid link structure is unprofitable. It is better to describe the routing rules at least as in Yii (just regular expression patterns).
See how routing is implemented in Ruby on Rails, nothing prevents you from implementing the same in PHP
See how it is done in CMF laravel.com/docs/routing , a very flexible and convenient structure is obtained.
I have true for java.
routes = new ArrayList<RouteData>();
routes.add(new RouteData("GET", "home/index/{^[0-9]{1,15}$}/valid/{^[A-z]{3,15}$}"));
routes.add(new RouteData("POST", "home/index/{^[0-9]{1,15}$}/valid/{^[A-z]{3,15}$}"));
Controller/Action/id
. But something more flexible (especially with regular expressions describing the parameter)Controller/Action/{id}
Controller/{id}/Action
Controller/Action/{name}/delete/{id}
Question/Read/{name}/{id}
+ the request method must also match. User/Profile/{name}/delete/{id}
The delete parameter in the url does not play any role, it is just for convenience and understanding what the URL does.
I made it quite simple in my cms and it works like a clock.
Everything is like this:
When the page is initialized, the parseBaseURL () method twitches in it, the base variables are isolated from $ _SERVER ["REQUEST_URI"]. And the base ones for the engine are /ru/section/document.html
That is, the current language is determined, if it is not specified, it is taken by default, then the section, and the document if it exists.
Further. Here's an example: a visitor clicked on the url /news/ - this is our section of the site. It has a module in it. Everything that will be built further, for example /news/page-1/, /news/view-1/, /news/download/file.zip or whatever, and anything can be in the url. How do I recognize this. The basic analysis was still at initialization, further, as I said, the section twitches, and the module is attached to it. In the module, I can set any template for the url.
For example:
$u_goods->addURL("#/cat-(\d+)/?#i", "category");//add cat
$u_goods->addURL("#/goods-(\d+)/?# i", "goods");//add goods
$u_goods->addURL("#/page-(\d+)/?$#i", "p");//add pages var
And that's it. Already in the class, the variables category, goods, p become available. Etc.
In total, the url can be anything, and you can always set your own rule in the module for any corner.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question