CMS routing in MVC

I am creating my own MVC framework in php as a tool to learn more advanced programming. I have a framework and it works, however I have a problem regarding the current routing method. I want the framework to support backend cms to complement the foreground website. The problem is that my routing structure works like this: mywebsite.com/controller/method/id The routing mechanism sorts the information into an array like this

 segments 0 => controller, 1 => method, 2 => id 

Right now, if I visit mywebsite.com/projects, it takes me to what I created as an admin page. Not only do I want this to be available only with mywebsite.com/admin/projects, I want mywebsite.com/projects to draw me to the interface.

So, if I want to visit mywebsite.com/projects , I would like it to display the "front" controller by clicking "projects" in this method. If I am at mywebsite.com/admin/projects , I would like it to load the project controller.

Here, the current routing class as a whole is as follows.

 <?php class Request { //url domain.com/controller/method/other //holds url segments 0 => controller, 1 => method, 2 => other, etc public $segments; function __construct() { $this->parse_globals(); } function parse_globals(){ //$uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI'])); $uri = (empty($_GET['rt'])) ? '' : $_GET['rt']; $this->segments = array_filter(explode('/', $uri)); if (in_array("admin", $this->segments)) { array_shift($this->segments); } print_r($this->segments); //remove index php if( reset($this->segments) == 'index.php') { array_shift ($this->segments); } } function controller(){ return $this->segment(0); } function method(){ return $this->segment(1); } function param( $str ){ return isset($_REQUEST[$str]) ? $_REQUEST[$str] : false; } function segment( $no ){ return isset($this->segments[$no]) ? $this->segments[$no] : false; } } 
0
source share
3 answers

Instead of just using explode() to separate URL segments, you can use a set of regular expression patterns.

For example, this next patter will try to match the firsts action , and if action exists, check to see if there is a controller in front of it:

 '/(:?(:?\/(?P<controller>[^\/\.,;?\n]+))?\/(?P<action>[^\/\.,;?\n]+))?/' 

Most PHP frameworks use different methods for creating such templates with simplified notation. Thus, you can determine which parts for each template are mandatory and which are optional. In addition, spare values ​​for additional parts can be provided.

That said ...

Before you start to do something as complex as cms with a framework, you can spend some extra time researching OOP. I would recommend at least a lecture from Mishko Hevery and Robert C. Martin. Just because you think you know how to write a class does not mean that you understand object-oriented programming.

Update

I have listed several materials in this answer . You may find them useful,

In addition, there are two more lectures that were not in the answer above:

+4
source

I understand that there are three cases of routing:

Main:

 /<controller>/<action>/<parameters> 

Special for admin panel (where "admin" will be a kind of separate module):

 /<module>/<controller>/<action>/<parameters> 

And finally, a special case for "/ projects", which maps to "/ front / projects".

In this case, you need to make your routing class more flexible so that it can handle any routing scheme. In a framework such as Kohana, this will be done with rules such as:

 Route::set('adminModule', 'admin/projects') ->defaults(array( 'controller' => 'projects', 'action' => 'admin', )); Route::set('projectPage', 'projects') ->defaults(array( 'controller' => 'front', 'action' => 'projects', )); Route::set('default', '(<controller>(/<id>(/<action>)))') ->defaults(array( 'controller' => 'index', 'action' => 'index', )); 

Obviously, this is just an example, but you get the idea. Basically, you want to provide reasonable default routing (e.g. controller / action / id), but you should also allow users to configure other routes.

+1
source

I am currently developing an extremely high performance php router. you might want to take a look.

https://github.com/c9s/Pux

We also provide a template compiler that does the same as yours. but you can write a simpler path instead of complex patterns.

for example, you can write something like this:

  /product/:id/:name [ id => '\d+', name => '\w+' ] 

FYI:

Pux is 48.5 times faster than the symfony router when sending static routes, 31 times faster in dispatching regular expressions. (with pux extension installed)

Pux tries not to use the computation time to dynamically build all the routes (for example, Symfony / Routing). Instead, Pux compiles your routes into a simple PHP cache array, compiled routes can be loaded from the cache very quickly.

With support for Pux PHP Extension, you can download and send routes 1.5 ~ 2x faster than pure PHP Pux.

-1
source

All Articles