CodeIgniter路由问题以访问前端和后端文件夹 [英] CodeIgniter routes issues to access the frontend and backend folder
问题描述
我正在使用CodeIgniter。我在控制器和视图中有frontend和backend文件夹。我尝试了服务器步骤,甚至检查了几乎所有解决方案,但仍然无法通过默认控制器访问它
routes.php
$ route ['default_controller'] ='frontend / User_control';
$ route ['404_override'] =‘’;
$ route ['translate_uri_dashes'] =否;
/ *后端********************************* /
$ config ['backend'] ='backend / Access_control';
1)我的问题是当我访问Url时
2)如何访问我尝试过的后端URL
前端用户控件
<?php
defined('BASEPATH')或exit('不允许直接脚本访问');
类User_control扩展了CI_Controller {
public $ current_date;
函数__construct()
{
parent :: __ construct();
$ this-> load-> library(’form_validation’);
$ this-> load-> helper(’form’);
}
公共功能index(){
$ this-> load-> view(’frontend / login’);
}
}
?>
后端访问控制
<?php
defined('BASEPATH')或exit('不允许直接脚本访问');
类Access_control扩展了CI_Controller {
函数__construct()
{
parent :: __ construct();
$ this-> load-> library(’form_validation’);
$ this-> load-> helper(’form’);
}
公共功能index(){
$ this-> load-> view(’backend / login’);
}
}
?>
已编辑
当我使用以下步骤时,它正在工作。我在控制器中添加了Test.php文件并更改了路由,然后获得了登录页面。
Test.php
<?php
defined('BASEPATH')或exit('不允许直接脚本访问');
类测试扩展了CI_Controller {
public $ current_date;
函数__construct()
{
parent :: __ construct();
$ this-> load-> library(’form_validation’);
$ this-> load-> helper(’form’);
}
公共功能index(){
$ this-> load-> view(’frontend / login’);
}
}
?>
routes.php
$ route ['default_controller'] ='测试';
希望这对您有所帮助:
内置的 $ route ['default_controller']
不适用于子文件夹。您必须按照如下要求扩展系统路由器:
您需要在其中创建 MY_Router.php
应用程序>核心> MY_Router.php
<?php
类MY_Router扩展CI_Router {
受保护的函数_set_default_controller(){
if(empty($ this-> default_controller)){
show_error('无法确定应显示的内容尚未在路由文件中指定默认路由。');
}
//是否指定了方法?
if(sscanf($ this-> default_controller,'%[^ /] /%s',$ class,$ method)!== 2){
$ method =‘index’;
}
//这就是我添加的内容,检查类是否为目录
if(is_dir(APPPATH.'controllers /'.$ class)){
//将类设置为目录
$ this-> set_directory($ class);
// $ method是类
$ class = $ method;
//如果设置了方法,则重新检查斜线
if(sscanf($ method,'%[^ /] /%s',$ class,$ method )!== 2){
$ method ='index';
}
}
if(!file_exists(APPPATH.'controllers /'.$ this-> directory.ucfirst($ class)。'。php')){
//这会在以后触发404
返回;
}
$ this-> set_class($ class);
$ this-> set_method($ method);
//分配路由的段,索引从1开始
$ this-> uri-> rsegments = array(
1 => $ class,
2 => $ method
);
log_message(调试,不存在URI。设置了默认控制器。);
}
}
这将允许您使用 $ route ['default_controller'] ='frontend / User_control';
作为默认控制器
I am using CodeIgniter. I have frontend and backend folder inside controllers and views. I tried server steps even check almost all the solution but still I am not able to access it my default controller
routes.php
$route['default_controller'] = 'frontend/User_control';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
/*backend*********************************/
$config['backend'] = 'backend/Access_control';
1) My issue is When I am accessing the Url http://localhost/example_ci_row/
I am getting 404 Page not found
2) How to access the backend URL I tried http://localhost/icube_row/admin but I am getting the error
frontend User_control
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_control extends CI_Controller {
public $current_date;
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('frontend/login');
}
}
?>
Backend Access control
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Access_control extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('backend/login');
}
}
?>
Edited
It's working when I use below steps. I added the Test.php file in the controller and change the routes then I am getting the login page.
Test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public $current_date;
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('frontend/login');
}
}
?>
routes.php
$route['default_controller'] = 'Test';
Hope this will help you :
The built in $route['default_controller']
will not work for sub-folders. you have to extend system router as per your requirements like this :
You need to create a MY_Router.php
in 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.');
}
}
This will allow you to use $route['default_controller'] = 'frontend/User_control';
as your default controller
这篇关于CodeIgniter路由问题以访问前端和后端文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!