问题
In my project I am trying to create one section for admin. Following http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter#top I tried the second method. According to it, my folder structure is changed to somewhat like this.
project
cache
config
controllers
-admin
index.php
-blog.php
system
views
-admin
index.php
blog.php
...................
I have created one controller index.php inside controllers/admin with following code:
class Index extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view(index/index);
}
}
And inside views/admin I have created a file index.php and echoed some string and in routes.php I have added this line,
$route['admin'] = 'admin/index';
But when I run the admin panel using the url, http://localhost/workspace/project/admin/
, I am getting the 404 error
The requested URL /workspace/project/admin/ was not found on this server.
What I am doing wrong? Is there any other settings I have to make.
Can someone please guide me to fix this ? I am new to Codeigniter.
Thanks in advance.
回答1:
EDIT- change routes to admin to this in routes.php
$route['admin/(:any)'] = "admin/$1";
Also, place this above your routes to default controller-
$route['default_controller'] = "welcome";
$route['404_override'] = '';
I hope it works! ////////////////////////////////////////////
I guess, something is wrong with your structure, If i am not wrong then its something like this-
project
cache
config
controllers
-admin // your admin folder
index.php // your default controller
-blog.php
system
views
-admin
index.php
blog.php
change the controller name from index to admin.php Hence the routes should be-
$route['admin'] = 'admin/admin/index'; // its like folder/controller/function
回答2:
let change your files structure (You need to create an index function in your admin.php controller)
project(may be root folder of CI)
applications
cache
config
controllers
-admin
index.php (method index)
-blog.php
system
views
-admin
index.php
blog.php
also change controller name to admin and view load
class Index extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('admin/index');
}
}
then change your route
$route['admin'] = 'admin/index';
回答3:
I prefer that you have 2 codeigniter projects one for admin and one for blog. As there would be common folders like images, css and js which would conflict.
There will also be salablility issues like common controller names as namespace in codeigniter is still a lengthy process to follow.
回答4:
try this
class Index extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('admin/index');
}
}
来源:https://stackoverflow.com/questions/22828024/setting-up-admin-panel-in-codeigniter