B
B
bad_guy2012-08-16 00:57:06
PHP
bad_guy, 2012-08-16 00:57:06

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

5 answer(s)
S
Sergey, 2012-08-16
Protko @Fesor

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).

R
Renat Ibragimov, 2012-08-16
@MpaK999

See how routing is implemented in Ruby on Rails, nothing prevents you from implementing the same in PHP

B
Big_Shark, 2012-08-16
@Big_Shark

See how it is done in CMF laravel.com/docs/routing , a very flexible and convenient structure is obtained.

F
FanKiLL, 2012-08-16
@FanKiLL

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}$}"));

I once decided to keep the parameters in {} brackets, so the parser knows that this is a parameter + you can put a regular expression there so that the routing matches exactly. Plus, holding in brackets, you can do routing not according to the pattern 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}

Plus, in my example, the request method (POST, GET DELETE...) also matters.
The router parser parses the incoming request and looks for whether we have such a routing, at the first match, for example
Question/Read/{name}/{id}+ the request method must also match.
Controller = Question, Action = Read will start with the parameters name, id
Of course, with the help of reflection, it is checked whether there is such a controller and an action method with such parameters, if not - 404
In general, something like this, it's hard to describe, if you ask something. But I think the principle is clear.
Basically, this was done so that various garbage could be put into the routing, which does not affect the request and is not a parameter, but which must be present.
Http Method Delete: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.

T
tarya, 2012-08-16
@tarya

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 question

Ask a Question

731 491 924 answers to any question