多个控制器?

时间:2018-04-01 00:24:15

标签: php url-routing

我正在为我的PHP项目使用MVC框架,它的工作原理如下: 我们的根就像http://localhost/myproject

它接受这样的网址:http://localhost/myproject/example-controller/example-function/params

它的作用是它转到示例控制器并执行函数example-function,我们可以在其中添加http://localhost/myproject/example-controller/example-function/1/2/3等参数

所以我有一个仪表板,我希望能够将crud功能应用于用户和文章,以便网址如下: http://localhost/myproject/dashboard/users,这意味着它将执行功能用户,但是我想创建一个新用户,然后链接变为dashboard / users / create,但它会将create视为参数而不是需要执行的实际函数..所以我理解我必须创建一个UserController和ArticleController,但这意味着链接将成为user / create或article / create,在这种情况下它不再使用DashboardController。

如果有人理解我试图描述的内容,可能会有人就如何解决这个问题提出可能的解释。

1 个答案:

答案 0 :(得分:-2)

我找到了解决问题的方法并重新编写了整个splitUrl()函数,因此它现在接受了我需要的内容。

function splitUrl() 
{
    if (isset($_GET['url'])) {        
        require(ROOT. 'core/routes.php');

    $tmp_url = trim($_GET['url'], "/");
    $tmp_url = filter_var($tmp_url, FILTER_SANITIZE_URL);

    $tmp_url = explode("/", $tmp_url);
    $param = ['params' => [], 'found' => false];
    foreach ($routes as $route => $value) {
        //echo("We iterate over a new route called <strong>". $route. "</strong><br>");
        $tmp_route = explode("/", $route);

        // Variables for the length of the arrays;
        $route_count = count($tmp_route);
        $url_count = count($tmp_url);

        //echo("The route count = <strong>". $route_count . "</strong> and the url count = <strong>". $url_count. "</strong><br>");

        // Check if the length of the route in our acceptable routes array is equal to the length of the url. 
         if ($route_count == $url_count) {
             $indexCount = null;
             for ($i = 0; $i < $route_count; $i++) { 
                 if ($tmp_route[$i] == '{param}') {
                     $indexCount = $i;
                     //echo("A parameter has been found at index <strong>" . $indexCount . "</strong><br>");
                     break;
                 }
             }

             if ($indexCount == null) {
                 $indexCount = count($tmp_route); 
                //echo("A parameter has NOT been found so the index has been set to <strong>". $indexCount. "</strong><br>");
             }

            $doContinue = false;
             for ($i = 0; $i < $indexCount; $i++) { 
                 //echo("We are comparing value <strong>". $tmp_route[$i] . "</strong> to <strong>". $tmp_url[$i] ."</strong><br>"); 

                 if ($tmp_route[$i] == $tmp_url[$i]) {
                     //echo("Value <strong>". $tmp_route[$i] ."</strong> appears to be valid<br>");
                     continue;
                 } else {
                     //echo("Value <strong>". $tmp_route[$i] ."</strong> appears to be invalid<br><hr>");
                     $doContinue = true;
                     break;
                 }
             }

             if ($doContinue) continue;

             $count = 0;
             for ($i = 0; $i < $route_count; $i++) { 
                 if ($tmp_route[$i] == $tmp_url[$count]) {
                     $count++;
                    continue;
                 } else {
                     array_push($param['params'], $tmp_url[$count]);
                    $count++;
                 }
             }
        } else {
            // This means the route that we are iterating over right now can not possibly be the one we are looking for as the length is not equal to the length of the url.
            continue;
        }

        $param['controller'] = explode("@", $value)[0];
        $param['action'] = explode("@", $value)[1];

        return $param;
     }
}