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

问题描述

我正在用 php 创建我自己的 MVC 框架,作为学习更高级编程的一种方式.我已经启动并运行了框架,但是我对当前的路由方法有疑问.我希望框架支持后端 cms 来赞美前端网站.问题是我的路由结构是这样工作的 - mywebsite.com/controller/method/id路由引擎将信息排序成这样的数组

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

现在,如果我访问 mywebsite.com/projects,它会将我带到我设置为管理页面的页面.我不仅希望它只能从 mywebsite.com/admin/projects 访问,我还希望 mywebsite.com/projects 将我带到前端.

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.

因此,如果我想访问 mywebsite.com/projects,我希望它呈现前端"控制器,将项目"推送到方法中.如果我访问 mywebsite.com/admin/projects,我希望它加载项目控制器.

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;
    }
}

推荐答案

不是简单地使用 explode() 来分隔 URL 的片段,您可以使用一组正则表达式模式.

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

例如,下面的模式会首先尝试匹配 action,并且,如果 action 存在,则检查是否设置了 controller在它之前:

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]+))?/'

大多数 PHP 框架使用不同的方式来生成这样的模式,并使用简化的符号.通过这种方式,您可以设置每个模式的哪些部分是必需的,哪些部分是可选的.并且还可以为可选部分提供回退值.

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.

也就是说……

在开始使用框架制作像 cms 这样复杂的东西之前,您可能会花一些额外的时间来研究 OOP.我建议至少观看 Miško Hevery 和 Robert C. Martin 的讲座.仅仅因为您认为您知道如何编写类,并不意味着您了解面向对象编程.

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.

更新

我在这个答案中列出了一些材料.您可能会发现它们很有用,

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:

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

08-15 00:39