0

我不太确定这是否是通过重写 URL 来完成的,但我不知道如何相应地命名它。

这是我的问题。我目前正在做一个网站项目,我正在使用 MVC 框架的抽象(它主要用于学习框架)。这就是我的文件夹结构的样子:

/controller/
 |--indexcontroller.php

/myaccount/
 |--/controller/
    |--indexcontroller.php
 |--index.php

/globals/
 |--framework.php

/templates/

/options/
 |--settings.php
 |--config-www.php.inc

所以目前我正在使用自动加载器来加载所需的类。myaccount 文件夹中的 index.php 继承了应该处理类加载的框架类:

$urlparts = explode("/", $_SERVER['REQUEST_URI']);
    $urlparts2 = explode("?", $urlparts[2]);
    $class = str_replace(".php", "", $urlparts2[0]);
    
    if ($class == "") {
        
        $class = "index";
    }

    $letters_first = substr($class, 0, 1);
    $letters_last = substr($class, 1, strlen($class));
    
    $class = strtoupper($letters_first).$letters_last."Controller";

    if (class_exists($class)) {

        $object = new $class();
        
    } else {
       
        echo "Problem: class $class does not exist.";
    }

我目前遇到的问题是,我只能使用“http://www.url.com/myaccount/”,它正在从 myaccount 加载控制器文件夹中的 indexcontroller.php(这很好)。但我也希望能够使用“http://www.url.com/myaccount/profile”,然后应该调用来自 myaccount 的控制器文件夹中的“profilecontroller.php”。

我怎样才能做到这一点?网址重写?还是我做错了?

4

1 回答 1

1

起初这可能有点让人不知所措,但如果你明白了,它会让你的生活更轻松。

1 ) 看看现有的框架,如 symfony、codeigniter 等,看看它们是如何完成路由的。

2)尝试通过composer重用这些“组件”(如路由表单symfony),这样您就不必自己动手了。

这边走:

  • 您不必自己编写所有内容。
  • 你正在使用热门的东西,比如作曲家:)
  • 您仍然可以通过查看其他框架的实现来学习

我希望这个能有一点帮助 :)。

于 2013-01-22T22:22:39.213 回答