在没有URL重定向的情况下调度到Kohana中其他控制器的控制器?

时间:2013-03-26 10:06:20

标签: controller kohana routes

我正在伟大的Kohana框架上编写CMS,管理员用户可以在后端选择几种类型的页面,并将其添加到具有完全自定义URL的菜单中。

Example of custom URL : "mmmm/about" or "aaa/bbb/release"

我有一个数据库表,用于存储每个URL应显示的内容:

Example : 
URL : mmm/about     Pagekind : Content page ; (Page) Id : 7
URL : aaa/release   Pagekind : News         ; (News) Id : 1

每种页面类型都有自己的控制器(因此它更干净,更易于管理,更容易添加新页面类型):

Examples :
>>> In /application/classes/Controller/Pages :
class Controller_Pages() extends Controller_Template { 
    public function action_index(){
        ...
    }
}
>>> In /application/classes/Controller/News :
class Controller_News() extends Controller_Template { 
    public function action_index(){
        ...
    }
}

我有这样的路线将所有内容发送到我的菜单控制器:

Route::set( 'menu_path', '(<menupath>)', array('menupath' => '.*?') )
->defaults(
    array(
        'controller' => 'menu',
        'action' => 'dispatch'
    )
);

我的菜单控制器应该执行调度:

>>> In /application/classes/Controller/Menu :
class Controller_Menu() extends Controller_Template 
{ 
    public function action_dispatch()
    {

        // Get the path :
        $menupath = $this->request->param('menupath');

        // Get page info
        $obj = ORM::factory('tablemenu')->where('URL','=',$menupath)->find();

        // Regarding to page kind, call the appropriate controller
        switch ($obj->Pagekind)
        {
           case 'Content page' : 
              // Call controller Page with $obj->Id as page id
              // ???????? 
              // ???????? 
              break;

           case 'News' : 
              // Call controller News with $obj->Id as news id
              // ???????? 
              // ???????? 
              break;

        }

    }
}

所以我陷入了呼叫控制器部分。我的问题是:
   - 有没有办法在控制器内调用控制器? (没有重定向的SEO)
   - 如果是的话,它干净吗?可靠吗?
   - 还有另一种方法吗?就像覆盖Route类一样?

由于

1 个答案:

答案 0 :(得分:6)

你有两个选择,我认为两者都可以被认为是干净可靠的。

1。内部申请

Kohana允许制作internal calls,因此您无需进行任何重定向即可实现您的目标。

class Controller_Menu() extends Controller_Template 
{ 
    public function action_dispatch()
    {
        // Get the path :
        $menupath = $this->request->param('menupath');

        // Get page info
        $obj = ORM::factory('tablemenu')->where('URL','=',$menupath)->find();

        // Regarding to page kind, call the appropriate controller
        switch ($obj->Pagekind)
        {
            case 'Content page' : 
                $url = 'pages/action/' . $obj->Id;
                break;

            case 'News' : 
                $url = 'news/action/' . $obj->Id;
                break;
        }

        // Override current response
        $this->response = Request::factory($url)->execute();
    }
}

请注意,这只是一个快速尝试,向您展示这可能如何工作(并且它本身可能不会)。请注意网址的action部分,您需要在某个时刻对其进行定义才能使其正常工作。 不过,实际上有一条路线可以匹配你在这里做的请求,这一点很重要。 您还需要提出一个解决方案,了解如何通过'menu_path'路径进行这些内部请求。但既然你还没有尝试过任何东西,我会让你玩它,如果你自己想出来的话。 :)

2。路线过滤器(推荐)

我假设您正在使用Kohana 3.3.x 作为项目,因为路由过滤器仅适用于此版本。

您可以在Route Filter内处理请求,而不是将所有请求路由到菜单控制器。

您的路线可能如下所示:

Route::set('menu_path', '(<menupath>)',
    array(
        'menupath' => '.*?')
    )
    ->filter(function($route, $params, $request)
    {
        $menupath = $params['menupath'];

        // Get page info
        $obj = ORM::factory('tablemenu')->where('URL','=',$menupath)->find();

        if ($obj->loaded())
        {
            // Regarding to page kind, call the appropriate controller
            switch ($obj->Pagekind)
            {
                case 'Content page' : 
                    $params['controller'] = 'Page';
                    $params['id'] = $obj->Id;
                    break;

                case 'News' : 
                    $params['controller'] = 'News';
                    $params['id'] = $obj->Id;
                    break;
            }

            return $params;
        }

        return FALSE;
    })
    ->defaults(array(
        'action'    => 'index',
    ));

请注意,此处无需定义默认控制器,因为如果找不到页面,您希望返回404错误。 我们确实想要定义默认操作,因为我无法在示例中的任何位置看到它。

您可能需要考虑使用与控制器名称匹配的Pagekind值,然后您不需要每种类型的case,您只需执行以下操作:

if ($obj->loaded())
{
    $params['controller'] = $obj->Pagekind;
    $params['id'] = $obj->Id;

    return $params;
}

路由过滤器中的这种复杂性(不仅仅是几行)我还建议将其保留在外面,作为单独的回调函数(如@Daan在下面的评论中所建议的那样),例如:

Route::set('menu_path', '(<menupath>)',
    array(
        'menupath' => '.*?')
    )
    ->filter('MyRouteHelper::myfilter')
    ->defaults(array(
        'action'    => 'index',
    ));

希望这会有所帮助:)