[英]PHP CodeIgniter - How to set default page for website root
我是CodeIgniter的新手,他使用2個嵌套的控制器目錄backend
和frontend
構建網站。 除了root以外,我所有的頁面都工作正常。 當沒有在url中指定路徑(僅網站根目錄)時,我面臨設置默認頁面的困難。
如果網站根目錄后沒有路徑,我想呈現主頁。 我把$route['default_controller'] = 'frontend/pages'
放$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);
}
}
希望這個能對您有所幫助 :
在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';
改變這個
$route['default_controller'] = 'frontend/pages';
改成
$route['default_controller'] = 'frontend/pages';
$route['default_controller'] = "pages";
嘗試通過以下方式更改控制器:
public function index()
{
$this->load->view('home');
}
默認情況下,在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';
如何在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.');
}
}
您可以使用以下方法覆蓋404默認機制:
$route['404_override'] = 'your_custom_or_default_page';
和默認控制器為:-
$route['default_controller'] = 'your_custom_or_default_page';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.