Codeigniter控制器和路由

问题描述 投票:0回答:2

我正在尝试使用codeigniter构建一个非常简单的Web应用程序,现在我正在尝试为网站和管理区域构建静态页面。

但任何路由都在给予

404页

除了家庭控制器。

我有2个包含网页的主文件夹,一个用于用户

application/views/pages/home.php

另一个是管理员。

application/views/admin/dashboard.php

我可以访问主页,但我无法访问管理页面。

这是Pages控制器:

<?php
class Pages extends CI_Controller {
  public function view($page = 'home')
  {
    $this->load->helper('html');
          if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
          {
             show_404();
          }

          $data['title'] = ucfirst($page); // Capitalize the first letter
          $this->load->view('templates/header', $data);
          $this->load->view('pages/'.$page, $data);
          $this->load->view('templates/footer', $data);
  }
}
?>

这是管理员控制器

<?php
class Admin extends CI_Controller {
  public function view($admin = 'dashboard')
  {
          if ( ! file_exists(APPPATH.'views/admin/'.$admin.'.php'))
          {
                show_404();
          }
          $data['title'] = ucfirst($page);/* should be there*/
          $this->load->view('admin/'.$admin, $data);
  }
}
?>

这是路线

$route['admin'] = 'admin/dashboard';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['translate_uri_dashes'] = FALSE;

我可以访问主页,但我无法访问管理页面。有什么帮助吗?

php codeigniter codeigniter-3
2个回答
1
投票

希望对你有帮助 :

确保你已经加载url助手和.htaccess并在config.php中设置base_url

你的config.php

$config['base_url'] = 'http://localhost/project_folder/';
$config['index_page'] = '';

在autoload.php中

$autoload['helper'] = array('url');

.htaccess文件应该是这样的:

Options +FollowSymlinks -Indexes
RewriteEngine on
DirectoryIndex index.php
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

.htaccess文件放在project_folder中

更换

$route['admin'] = 'admin/dashboard';

有了这个 :

$route['admin'] = 'admin/view';

route.php应该是这样的:

$route['admin'] = 'admin/view';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['translate_uri_dashes'] = FALSE;

访问网址如下:

http://localhost/project_folder/
http://localhost/project_folder/admin

更多信息:https://www.codeigniter.com/user_guide/tutorial/static_pages.html


0
投票

我认为代码应该是这样的..如果我已经纠正了你的部分错误,希望这对你有用.....

<?php
class Admin extends CI_Controller {
  public function view($admin = 'dashboard')
  {
          if ( ! file_exists(APPPATH.'views/admin/'.$admin.'.php'))
          {
                show_404();
          }else{
           $data['title'] = ucfirst($page);/* should be there*/
          $this->load->view('admin/'.$admin, $data);
          }

  }
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.