PHP CodeIgniter - 如何设置网站root的默认页面

时间:2018-05-19 09:50:33

标签: php codeigniter codeigniter-3

我是CodeIgniter的新手,使用2个嵌套控制器目录backendfrontend构建网站。我的所有页面都工作正常,除了root。当url中没有指定路径(仅限网站根目录)时,我将面临设置默认页面的困难。

如果网站root之后没有路径,我想呈现主页。我把$route['default_controller'] = 'frontend/pages',但它不起作用。

我的config/routes.php如下:

$route['default_controller'] = 'frontend/pages';

$route['user'] = 'user/index';
$route['user/register']['GET'] = 'frontend/user/index';

$route['user/register']['POST'] = 'frontend/user/register_user';
$route['user'] = 'frontend/user/login_view';
$route['user/login'] = 'frontend/user/login_view';
$route['user/login_user'] = 'frontend/user/login_user';
$route['user/user_profile'] = 'frontend/user/user_profile';
$route['user/user_logout'] = 'frontend/user/user_logout';

$route['admin'] = 'backend/Admin_area/dashboard';
$route['admin/(index|dashboard)'] = 'backend/Admin_area/dashboard';
$route['admin/(:any)/(:any)/(:any)'] = 'backend/$1/$2/$3';
$route['admin/(:any)/(:any)'] = 'backend/$1/$2';
$route['admin/(:any)'] = 'backend/$1';

$route['/^$'] = 'frontend/pages/view/home';
$route['(:any)'] = 'frontend/pages/view/$1';

当我访问root/时,请告诉我404 error page

Pages控制器代码:

<?php
class Pages extends Frontend_Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $page = 'home';
            $this->data['pagetitle'] = ucfirst($page); 

            $this->render('pages/'. $page);
    }

        public function view($page = 'home')
    {

            $this->data['pagetitle'] = ucfirst($page); 

            $this->render('pages/'. $page);
    }
}

5 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

在CodeIgniter 3上它不允许您在$route['default_controller']上拥有一个子文件夹,而是需要创建一个MY_Router。php文件,如下所示。

您需要在

中创建一个MY_Router.php
application > core > MY_Router.php

class MY_Router extends CI_Router {
protected function _set_default_controller() {

    if (empty($this->default_controller)) {

        show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
    }
    // Is the method being specified?
    if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
        $method = 'index';
    }

    // This is what I added, checks if the class is a directory
    if( is_dir(APPPATH.'controllers/'.$class) ) {

        // Set the class as the directory

        $this->set_directory($class);

        // $method is the class

        $class = $method;

        // Re check for slash if method has been set

        if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
            $method = 'index';
        }
    }

    if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {

        // This will trigger 404 later

        return;
    }
    $this->set_class($class);
    $this->set_method($method);
    // Assign routed segments, index starting from 1
    $this->uri->rsegments = array(
        1 => $class,
        2 => $method
    );
    log_message('debug', 'No URI present. Default controller set.');
}
}

在route.php中

$route['default_controller'] = 'frontend/pages';

答案 1 :(得分:1)

更改此

$route['default_controller'] = 'frontend/pages';

更改为

$route['default_controller'] = 'frontend/pages';
$route['default_controller'] = "pages";

答案 2 :(得分:1)

尝试使用以下方式更改控制器:

 public function index()
    {
        $this->load->view('home');
    }

答案 3 :(得分:0)

默认情况下,在codeigniter中,默认控制器路由只能位于第一级控制器上。

所有其他控制器都可以在子文件夹中,但不是您想要的默认控制器。

正确

application
application > controllers
application > controllers > Pages.php // Your Default Controller

application
application > controllers
application > controllers > frontend > Pages.php
  

能够拥有default_controller路由的子文件夹

$route['default_controller'] = 'subfolder/controller/function';

How to use a sub folder in default controller route in CodeIgniter 3

您需要创建一个application / core / MY_Router.php文件

<?php

class MY_Router extends CI_Router {

    protected function _set_default_controller() {

        if (empty($this->default_controller)) {

            show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
        }
        // Is the method being specified?
        if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
            $method = 'index';
        }

        // This is what I added, checks if the class is a directory
        if( is_dir(APPPATH.'controllers/'.$class) ) {

            // Set the class as the directory

            $this->set_directory($class);

            // $method is the class

            $class = $method;

            // Re check for slash if method has been set

            if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
                $method = 'index';
            }
        }

        if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {

            // This will trigger 404 later

            return;
        }
        $this->set_class($class);
        $this->set_method($method);
        // Assign routed segments, index starting from 1
        $this->uri->rsegments = array(
            1 => $class,
            2 => $method
        );
        log_message('debug', 'No URI present. Default controller set.');
    }
}

答案 4 :(得分:-1)

您可以使用以下方式覆盖404默认机制: -

$route['404_override'] = 'your_custom_or_default_page';

默认控制器为: -

$route['default_controller'] = 'your_custom_or_default_page';
相关问题