本文介绍了CMS路由在MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的PHP我自己的MVC框架,学习更高级的编程手段。我已经得到了框架和运行但是我对当前的路由选择方法的问题。我想要的框架来支持后端CMS恭维前端网站。问题是,我的路由结构的工作原理是这样 - mywebsite.com/controller/method/id
路由引擎排序信息到像这样的数组

  0段= GT;控制器,1 =>法,2 => ID

现在,如果我访问mywebsite.com/projects需要我什么,我已经建立了作为一个管理页面。不仅我希望这是访问只能从mywebsite.com/admin/projects,我要mywebsite.com/projects带我去前台。

所以,如果我想访问 mywebsite.com/projects 我想它呈现前控制器,力推工程入法。如果我访问 mywebsite.com/admin/projects 我想它加载的项目控制器。

下面是整体当前的路由类,如下所示。

 < PHP一流的请求{    // URL domain.com/controller/method/other
    // URL持有细分0 =>控制器,1 =>法,2 =>其他的,等
    公共$段;
    功能__construct(){
        $这个 - > parse_globals();
    }    功能parse_globals(){
        // $ URI = preg_replace(| /(.*)|,\\\\ 1,str_replace函数(\\\\,/,$ _ SERVER ['REQUEST_URI']));
        $ URI =(空($ _ GET ['RT']))? '':$ _GET ['RT'];
        $这个 - >部分= array_filter(爆炸('/',$ URI));
        如果(in_array(管理员,这 - $>段)){
            array_shift($这个 - >段);
        }
        的print_r($这个 - >段);
        //删除指数PHP
        如果(复位($这个 - >段)=='的index.php'){
            array_shift($这个 - >段);
        }
    }    功能控制器(){
        返回$这 - >段(0);
    }    函数法(){
        返回$这 - >段(1);
    }    功能参数($ STR){
        返回使用isset($ _ REQUEST [$ STR])? $ _REQUEST [$ STR]:假的;
    }    功能段($无){
        返回使用isset($这个 - >段[$无])? $这个 - >段[$否]:假的;
    }
}


解决方案

而不是简单地使用爆炸的()来的URL的部分分开,你可以使用一组常规的前pression格局。

例如,这下面的行话会尝试以匹配首创动作,并在动作存在他们检查如果有控制器之前设置:

'/((:\\ /(P<控制器>?^ \\ / \\; \\ n] +))\\ /(P<?作用> [^ \\ / \\。吗?\\ n] +))/

大多数PHP框架用不同的方式来产生这样的模式,通过简化符号。这样,您可以设置在每个模式部分是必需的,哪些可选的。而且它也可以用于可选部件提供备用值。

这表示...

开始做的东西太复杂与框架CMS之前,你可能会投资一些额外的时间在研究OOP。我建议至少观看MISKO Hevery和罗伯特C.马丁讲座。仅仅因为你想,你知道如何写一个类,并不意味着你理解面向对象编程。

更新

我列举一些材料。您可能会发现它们非常有用,

此外这里有两个讲座,是不是在回答以上:




I am creating my own MVC framework in php as a means to learn more advanced programming. I've got the framework up and running however I have an issue regarding the current routing method. I want the framework to support a backend cms to compliment the front end website. The issue is that my routing structure works like so - mywebsite.com/controller/method/idThe routing engine 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 have set up as an admin page. Not only do I want this to be accessible only from mywebsite.com/admin/projects, I want the mywebsite.com/projects to take me to frontend.

So if I want to visit mywebsite.com/projects I'd like it to render the "front" controller, pushing "projects" into the method. If I visit mywebsite.com/admin/projects I'd like it to load the projects controller.

Here's the current routing class in whole 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;
    }
}
解决方案

Instead of simply using explode() to separate the segments of URL, you could use a set of regular expression pattern.

For example, this following patter would try to match at firsts action, and, if action exists them check if there is controller set before it:

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

Most of PHP frameworks use different ways to generate such patterns, with simplified notations. This way you can set which parts for each pattern are mandatory and which optional. And it is also possible to provide fallback values for the optional parts.

That said ...

Before starting to make something so complicated as cms with framework, you might invest some additional time in researching OOP. I would recommend to at least watch lectures from Miško Hevery and Robert C. Martin. Just because you think, that you know how to write a class, does not mean, that you understands object oriented programming.

Update

I have listed few materials in this answer. You might find them useful,

Additionally here are two more lectures, that were not in answer above:

这篇关于CMS路由在MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:39