久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方

Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
本文介紹了這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是 MVC 的新手,所以這是我的第一次嘗試,我相信你們可以在這方面給我改進,感謝您提供任何提示或幫助!

I am new to MVC so this is my first attempt and I am sure you guys can give me improvement on this, thanks for any tips or help!

以下是我為我的個人框架設計的路由器/調度程序系統,這是我第一次嘗試使用 MVC 模式.

Below is what I have come up with for a router/dispatcher system for my personal framework I am working on, it is my first attempt at using the MVC pattern.

第一塊代碼只是我的 .htaccess 文件,它通過我的 index.php 文件路由所有請求.

The first block of code is just my .htaccess file which routes all request through my index.php file.

第二個代碼塊是我的Routes"數組,它會告訴 Router 對象,調用哪個類和方法,以及任何 ID 或分頁號碼(如果存在).

The second block of code is my array of "Routes" which will tell the Router object, which class and method to call as well as any ID or paging numbers if they exists.

第三塊代碼是路由器類.

Third block of code is the router class.

第四塊只是運行類

因此,路由器類必須使用正則表達式將 URI 與路由映射中的路由匹配,理論上,當正則表達式必須運行在 50 多個路由的列表中時,這聽起來性能很差,應該我的做法不一樣?我使用正則表達式的主要原因是匹配路由中存在的頁碼和 ID 號.

So the router class has to use regex to match the URI with a route in the route map, in theory, this just sounds like bad performance when there is a list of 50+ routes that the regex has to run on, should I be doing this differently? The main reason I use the regex is to match page numbers and ID numbers when they exists in the route.

另外請不要只是告訴我使用一個框架,我這樣做是為了更好地學習它,我通過這種方式學習得更好,現在只是不想使用現有框架,我已經研究了所有主要框架,并且一些不太常見的想法已經.

Also please do not just tell me to use a framework, I am doing this to learn it better, I learn better this way and just prefer to not use an existing framework at this time, I have studies all the main ones and some less common ones for ideas already.

1) 那么主要問題是,有什么地方看起來不對嗎?

1) So the main question, does anything just not look right?

2) 有沒有比像我這樣在數組上使用正則表達式更好的方法來檢測 URI 中的內容,在高流量站點上考慮一下?

2) Is there a better way to detect what is in the URI than using the regex on an array like I am doing, consider it on a high traffic site?

3) 由于所有內容都通過 index.php 文件路由,我將如何處理 AJAX 請求?

3) Since everything is routed through the index.php file with this, how would I go about handling AJAX requests?

對不起,如果這令人困惑,我自己有點困惑!

Sorry if this is confusing, I am a little confused mtyself!

.htaccess 文件

.htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$   index.php?uri=$1    [NC,L,QSA]

映射數組()

/**
 * Map URI to class/method and ID and Page numbers
 * Must be an array
 */
$uri_route_map = array( 
    //forums
    'forums/' => array(
        'controller' => 'forums',
        'method' => 'index',
        'id_number' => '',
        'page_number' => ''),

    'forums/viewforum/(?<id_number>d+)' =>  array(
        'controller' => 'forums',
        'method' => 'viewforum',
        'id_number' => isset($id_number),
        'page_number' => ''),  

    'forums/viewthread/(?<id_number>d+)' =>  array(
        'controller' => 'forums',
        'method' => 'viewthread',
        'id_number' => isset($id_number),
        'page_number' => ''),

    'forums/viewthread/(?<id_number>d+)/page-(?<page_number>d+)' =>  array(
        'controller' => 'forums',
        'method' => 'viewthread',
        'id_number' => isset($id_number),
        'page_number' => isset($page_number)),

    // user routes
    // account routes
    // blog routes 
    // mail routes
    // various other routes
);

讀取并匹配上面Map數組的Router類

Router class that reads and matches the Map array above

/**
 * Run URI against our Map array to get class/method/id-page numbers
 */
 class Router
{
    private $_controller = '';
    private $_method = '';
    public $page_number = '';
    public $id_number = '';

    public function __construct($uri, array $uri_route_map)
    {
        foreach ($uri_route_map as $rUri => $rRoute)
        {
            if (preg_match("#^{$rUri}$#Ui", $uri, $uri_digits))
            {
                //if page number and ID number in uri then set it locally
                $this->page_number = (isset($uri_digits['page_number']) ? $uri_digits['page_number'] : null);
                $this->id_number = (isset($uri_digits['id_number']) ? $uri_digits['id_number'] : null);
                $this->_controller = $rRoute['controller'];
                $this->_method = $rRoute['method'];

                // just for debug and testing while working on it / will be removed from final code
                echo '<hr> $page_number = ' . $this->page_number . '<br><br>';
                echo '<hr> $id_number = ' . $this->id_number . '<br><br>';
                echo '<hr> $controller = ' . $this->_controller . '<br><br>';
                echo '<hr> $method = ' . $this->_method . '<br><br>';
                break;
            }else{
                $this->page_number = '';
                $this->id_number = '';
                $this->_controller = '404';
                $this->_method = '404';
            }
        }
    }

    public function getController()
    {
        return $this->_controller;
    }

    public function getMethod()
    {
        return $this->_method;
    }

    public function getPageNumber()
    {
        return $this->page_number;
    }

    public function getIDNumber()
    {
        return $this->id_number;
    }

    /**
     * Call our class and method from values in the URI
     */
    public function dispatch()
    {
        if (file_exists('controller' . $this->_controller . '.php'))
        {
            include ('controller' . $this->_controller . '.php');
            $controllerName = 'Controller' . $this->_controller;
            $controller = new $controllerName($this->getIDNumber(),$this->getPageNumber());
            $method = $this->_method;
            if (method_exists($this->_controller, $this->_method))
            {
                return $controller->$method();
            } else {
                // method does not exist
            }
        } else {
            // Controller does not exist
        }
    }

}

運行它

/**
 * Testing the class
 */
$uri = isset($_GET['uri']) ? $_GET['uri'] : null;
$router = new Router($uri, $uri_route_map);
$router->dispatch();

?>

推薦答案

1) 我覺得沒問題.不過代碼看起來有點亂.

1) Look alright to me. The code looks a bit messy though.

2) 是的,有更好的方法.您正在執行正則表達式,因為您想匹配您不知道的 URL 部分.為什么不做 $parts = purge("/", $uri) 然后看看你是否能找到你要找的頁面?您需要定義每個頁面需要多少個參數,否則您將不知道是選擇帶有參數 array("viewform", 123) 還是 forums>forums/viewforum 帶參數 array(123).

2) Yes there is a better way. You're doing the regex because you want to match parts of the URL that you don't know. Why not do $parts = explode("/", $uri) then see if you can find the page you're looking for? You will need to define how many parameters you're expecting for each page or you wont know whether to pick forums with parameters array("viewform", 123) or forums/viewforum with parameters array(123).

explode 感覺比正則表達式更好加載.它還增加了改進錯誤處理的好處.如果傳遞給 viewforum 的參數不是數字怎么辦?當然你可以比 "404" 做得更好;)

explode feels loads better than a regex. It also adds the benefit of improved error handling. What if the argument passed to viewforum is not a number? Surely you can do better than "404" ;)

3) 制作一個單獨的 ajax 處理程序.無論如何,Ajax 都是隱藏的,因此您無需費心提供語義 URL.

3) Make a seperate ajax handler. Ajax is hidden from view anyway so you don't need to bother with providing semantic URLs.

示例:

function find_route($parts) {
    foreach ($uri_route_map as $route => $route_data) {
        $route_check = implode("/", array_slice($parts, 0, count($parts) - $route_data['num_arguments']));
        if ($route_check === $route) {
            return $route_data;
        }
    }
    throw new Exception("404?");
}

$uri = "forum/viewforum/522";

$parts = explode("/", $uri);
$route = find_route($parts);
$arguments = array_slice($parts, count($parts) - $route['num_arguments']);

$controller = $rRoute['controller'];
$method = $rRoute['method'];
$controller_instance = new $controller();
call_user_func_array(array($controller_instance, $method), $arguments);

(未經測試)

插件

由于 $uri_route_map,您無??法動態"注冊更多插件或頁面或路由".我會添加一個函數來動態地向 Router 添加更多路由.

Because of $uri_route_map you can't 'dynamically' register more plugins or pages or 'routes'. I'd add a function to add more routes dynamically to the Router.

此外,您可以考慮一些自動發現方案,例如,將檢查文件夾 plugins/ 中包含名為manifest.php"的文件的文件夾,當調用該文件時,可以選擇添加更多路由到路由器.

Additionally you could consider some auto-discovery scheme that, for instance, will check the folder plugins/ for folders with a file called "manifest.php" that, when called, will optionally add more routes to Router.

這篇關于這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
Separation of concerns; MVC; why?(關注點分離;MVC;為什么?)
主站蜘蛛池模板: 国产欧美日韩一区二区三区在线观看 | 免费午夜视频 | 国产精品成人在线播放 | 久久99精品久久久 | 国产精品视频在线观看 | 国产.com| 成人a视频在线观看 | 精品成人一区二区 | 国产视频二区 | 久久九九99 | 一区二区在线 | 日本精品久久 | 成人免费av | 成人免费xxxxx在线视频 | 欧美日韩免费在线 | 一级黄色绿像片 | 日本亚洲欧美 | 久久精品一二三影院 | 午夜影视 | 中文字幕精品一区二区三区精品 | 日韩免费视频一区二区 | 日韩成人影院 | 日韩午夜在线观看 | 亚洲永久免费观看 | 日韩国产精品一区二区三区 | 成人午夜免费在线视频 | 亚洲免费网站 | 国产一二三区电影 | 91看片网 | 亚洲人va欧美va人人爽 | 亚洲精品久 | 亚洲精品99 | 夫妻午夜影院 | 狠狠天天 | 激情欧美日韩一区二区 | 天堂网中文 | 天堂中文字幕av | 久久久久九九九女人毛片 | 在线观看国产视频 | 欧美在线天堂 | 午夜丰满寂寞少妇精品 |