如何在控制器中使用控制器?

时间:2011-11-06 16:03:26

标签: php kohana kohana-3

我正在使用Kohana 3.2,我在另一个控制器中调用控制器的输出时遇到问题。

我想要的......

在某些页面中,我有一个菜单,而在其他页面中,我没有。我想利用HMVC请求系统的灵活性。在页面的控制器中,我想调用另一个负责创建菜单的控制器。

我现在拥有的一切:

file menu.php:

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Menu extends Controller 
{
    private $_model = null;

    public function __construct(Request $request, Response $response)
    {       
        parent::__construct($request, $response);
        $this->_model = Model::factory('menu');
    }

    public function action_getMenu()
    {
        $content = array();
        $content['menuItems'] = $this->_model->getMenuItems();

        // Render and output.
        $this->request->response = View::factory('blocks/menu', $content);
        //echo '<pre>'; print_r($this->request->response->render()); echo '</pre>'; die();
    }   
} 

somepage.php

public function action_index()
{
    $this->template->title = 'someTitle';;
    $contentData['pageTitle'] = 'someTitle';
    $contentData['contentData'] = 'someData';

    #include the menu       
    $menuBlock = Request::factory('menu/getMenu')->execute();
    $menuData =  array('menu' => $menuBlock);
    $this->template->menu = View::factory('pages/menu')->set('menu',$menuData);

    $this->template->content = View::factory('pages/somePage', $contentData);

    $view = $this->response->body($this->template);
    $this->response->body($view);
}

如果我取消注释menu.php中的以下行,我会看到呈现的菜单:

//echo '<pre>'; print_r($this->request->response->render()); echo '</pre>'; die();

所以我觉得那部分没问题。问题出在somepage.php中的以下行:

$menuBlock = Request::factory('menu/getMenu')->execute();

这给了我一个响应对象。无论我做什么,我都不会在$ this-&gt; template-&gt;菜单中获得输出。

$ this-&gt; template-&gt; menu = View :: factory('pages / menu') - &gt; set('menu',$ menuData);

我必须做什么让$ this-&gt; template-&gt;菜单包含视图,以便我可以正确使用它?

我希望这一切都有道理。这是我想要的方式,但也许我完全走错了路。

2 个答案:

答案 0 :(得分:2)

我会这样做:

class Controller_Menu extends Controller
{
    public function action_build()
    {
        // Load the menu view. 
        $view = View::factory('navigation/menu');
        // Return view as response-
        $this->response->body($view->render());
    }
}

在你的控制器中获取如下菜单:

// Make request and get response body.
$menu = Request::factory('menu/build')->execute()->body();
// e.g. assign menu to template sidebar.
$this->template->sidebar = Request:.factory('menu/build')->execute()->body();

我不会在控制器中使用__construct方法。使用before()代替,这对于大多数问题(例如auth)就足够了:

public function before()
{
    // Call aprent before, must be done here.
    parent::before();
    // e.g. heck whether user is logged in.
    if ( !Auth::instance()->logged_in() )
    {
        //Redirect if not logged in or something like this.
    }
}

答案 1 :(得分:1)

我在询问后不到一个小时就找到了问题的答案。 我只是忘了把它放在这里。

在somePage.php中更改:

$menuBlock = Request::factory('menu/getMenu')->execute(); 
$menuData = array('menu' => $menuBlock); 
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData); 

致:

$this->template->menu = Request::factory('menu/getMenuBlock')->execute()->body(); 

在menu.php中更改:

$this->request->response = View::factory('blocks/menu', $content); 

致:

$request = View::factory('blocks/menu', $content); 
$this->response->body($request); 

我希望这会帮助别人。