doushu5451 2014-03-15 15:30
浏览 29

如何使用MVC模式创建动态路由器?

I'm trying to build an url like http://example.com/username using mvc my own, I use the front controller like

<?php

namespace Core;

use Core\KliknklopException;
use Core\Config;

class Controller
{
    const DEFAULT_CONTROLLER = "\\Controllers\\IndexController"; 
    const DEFAULT_ACTION     = "indexAction";

    protected static $controller    = self::DEFAULT_CONTROLLER;
    protected static $action        = self::DEFAULT_ACTION;
    protected static $params        = array();

    public static function init(array $options = array()) {
        if (empty($options)) {
           self::parseUri();
        }
        else {
            if (!empty($options["controller"])) {
                self::$setController($options["controller"]);
            }
            if (!empty($options["action"])) {
                self::$setAction($options["action"]);    
            }
            if (!empty($options["params"])) {
                self::$setParams($options["params"]);
            }
        }
    }

    protected static function parseUri() {
        $path       = trim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), "/");
        $path       = preg_replace('[^a-zA-Z0-9/]', "", $path);
        $basePath   = Config::applicationConfig()['baseController'];

        if (strpos($path, $basePath) === 0) {
            $path = substr($path, strlen($basePath));
        }

        @list($controller, $action, $params) = explode("/", $path, 3);

        if (!empty($controller)) {
            self::setController($controller);
        }
        if (!empty($action)) {
            self::setAction($action);
        }
        if (!empty($params)) {
            self::setParams(explode("/", $params));
        }

    }

    public static function setController($controller) {
        $controller = "\\Controllers\\".ucfirst(strtolower($controller)) . "Controller";
        if (!class_exists($controller)) {
            throw new KliknklopException ("The action controller '$controller' has not been defined.");
        }
        self::$controller = $controller;
        return self::$controller;
    }

    public static function setAction($action) {
        if (!method_exists(self::$controller, $action . "Action")) {
            throw new KliknklopException ("The controller action '$action'Action has been not defined.");
            exit();
        }

        self::$action = $action. "Action";
        return self::$action;
    }

    public static function setParams(array $params) {
        self::$params = $params;
        return self::$action;
    }

    public static function run() {
        call_user_func_array(array(new self::$controller, self::$action), self::$params);
    }
}

then the controller, I created like this :

<?php

namespace Controllers;

class ProfileController {   


    public function __construct()
    {
    }

    public function userAction($name_user) {
        echo "hello {$name_user}";
    }
}

I can access the url http://example.com/profile/user/panji controller, how can I make a url like http://example.com/panji, do I have to use htaccess or using routing? Please help me. thanks

Regards.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值