使用HMVC将管理控制器清除

时间:2012-10-07 12:46:13

标签: codeigniter hmvc

我有HMVC的问题

我的所有模块中都有管理员控制器

- modules 

 - users 
     - controllers 
         - admin.php 
         - users.php
     - views
         - admin_create_user.php
         - admin_view_users.php
         - signup.php
         - login.php

 - news
     - controllers
         - admin.php
         - news.php
     - views
         - admin_disply_news.php
         - admin_create_news.php
         - view_news.php

现在当转到用户admin时,该URL将链接此

  • domain.com/users/admin/method。
  • domain.com/news/admin/method。

但我需要它

  • domain.com/admin/users/method
  • domain.com/admin/news/method

2 个答案:

答案 0 :(得分:1)

将这些代码添加到" core / MY_Router.php"内部" MY_Router"课程 - *(类MY_Router扩展MX_Router {** ---代码进入 - **})*

我尝试使用路由规则和.htaccess执行此操作但不使用它们。然后我编辑MX_Router代码并且工作正常,但有一件事要注意你必须在你的模块控制器调用' admin ' admin '并将控制器放在那里工作,因为这样你可以通过直接调用控制器来使用模块的默认路由,如果控制器名称与模块名称相同。

public $module;
private $located = 0;

protected function _set_request($segments = array()){
    $segments = $this->_validate_request($segments);
    // If we don't have any segments left - try the default controller;
    // WARNING: Directories get shifted out of the segments array!
    if (empty($segments))
    {
        $this->_set_default_controller();
        return;
    }

    if ($this->translate_uri_dashes === TRUE)
    {
        $segments[0] = str_replace('-', '_', $segments[0]);
        if (isset($segments[1]))
        {
            $segments[1] = str_replace('-', '_', $segments[1]);
        }
    }

    if($segments[0] == 'admin' && isset($segments[1])){
        if (isset($segments[2])){
            $this->set_method($segments[2]);
            $segments[2] = $segments[2];
        }else{
            $this->set_method('index');
            $segments[2] = 'index';
        }
        $this->directory    = '../modules/'.$segments[1].'/controllers/admin/'; 
        $this->module       = $segments[1];
        $this->class        = $segments[1];

        $segments[1] = $segments[1];


        unset($segments[0]);
        $this->uri->rsegments = $segments;
    }else{
        $segments = $this->locate($segments);

        if($this->located == -1)
        {
            $this->_set_404override_controller();
            return;
        }

        if(empty($segments))
        {
            $this->_set_default_controller();
            return;
        }

        $this->set_class($segments[0]);

        if (isset($segments[1]))
        {
            $this->set_method($segments[1]);
        }
        else
        {
            $segments[1] = 'index';
        }

        array_unshift($segments, NULL);
        unset($segments[0]);
        $this->uri->rsegments = $segments;  
    }   
}

答案 1 :(得分:0)

您可以尝试将其添加到路线配置文件中:

$route['domain.com/admin/users/(:any)'] = 'domain.com/users/admin/method';
$route['domain.com/admin/news/(:any)'] = 'domain.com/news/admin/method';

当用户输入 domain.com/admin/users/method 时,它会调用用户控制器。

Documentation