Codeigniter 3路线不工作
I am setting up a CodeIgniter project and so far everything has been going well. All my routes and controllers are working correctly except for one. While I am new to CodeIgniter I have been programming in PHP for close to five years so I have a bit of experience. If anyone could take a look at the code samples below and let me know what I am doing wrong, that would be great.
Apparently the application does recognize the route, but it keeps complaining about its inability to find the URI.
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Profile extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
}
public function Profile($page = 'profile-page')
{
if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
{
// Load error page
show_404();
}
// Capitalize the first letter
$data['title'] = ucfirst($page);
$data['pageType'] = "profile-page";
// Load pages
$this->load->view('templates/header', $data);
$this->load->view('templates/topbar', $data);
$this->load->view('profile');
$this->load->view('templates/footer', $data);
//
}
}
Next is my Routes:
$route['default_controller'] = 'home/home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// All custom routes go below this line.
$route['contact'] = 'pages/contact';
$route['signup'] = 'auth/signup';
$route['login'] = 'auth/login';
$route['profile'] = 'profile/profile';
I have verified that all the view files are in their correct folders and there are no typos. However the Profile route keeps failing. All the other routes work correctly, except for the Profile route.
I have tried this on Windows 10 with XAMMP, Mac OS X Sierra with MAMP and CentOS 7 with Apache/PHP/MySQL.
Thanks.
我正在设置一个CodeIgniter项目,到目前为止一切进展顺利。 我的所有路由和控制器都正常工作,除了一个。 虽然我是CodeIgniter的新手,但我已经用PHP编程了将近五年,所以我有一些经验。 如果有人可以查看下面的代码示例并让我知道我做错了什么,那就太棒了。 p>
显然应用程序确实识别了路线,但它一直在抱怨 它无法找到URI。 p>
控制器: p>
&lt;?php
defined('BASEPATH')或退出('否 直接脚本访问允许');
class配置文件扩展CI_Controller {
函数__construct(){
parent :: __ construct();
$ this-&gt; load-&gt; helper('url') ;
}
公共功能配置文件($ page ='profile-page')
{
if if(!file_exists(APPPATH。'/ views /'.$ page。'。php'))
{
//加载错误页面
show_404();
}
//大写第一个字母
$ data ['title'] = ucfirst($ page);
$ data ['pageType '] =“profile-page”;
//加载页面
$ this-&gt; load-&gt; view('templates / header',$ data);
$ this-&gt; load-&gt ; view('templates / topbar',$ data);
$ this - &gt; load-&gt; view('profile');
$ this-&gt; load-&gt; view('templates / footer',$ data);
//
}
}
pre>
接下来是我的路线: p>
$ route ['default_controller'] ='home / home';
$ route ['404_override'] ='';
$ route ['translate_uri_dashes'] = FALSE;
//所有自定义路线都在此行之下。
$ route ['contact'] ='pages / contact';
$ route ['signup'] ='auth / signup';
$ route ['login'] ='auth / login';
$ route ['profile'] ='个人资料/个人资料 ';
code> pre>
我已验证所有视图文件都在正确的文件夹中,并且没有拼写错误。 但是,配置文件路由仍然失败。 除了Profile路由之外,所有其他路由都能正常工作。 p>
我在Windows 10上使用XAMMP,Mac OS X Sierra使用MAMP和CentOS 7使用Apache / PHP / MySQL进行了尝试。 p>
谢谢。 p>
div>
The problem is your code enters into this condition & it is not able to find the profile-page.php file at that path and showing error.As profile function is calling properly without any issue as i checked
if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
{
echo "test";die;
// Load error page
show_404();
}
It's been a little while since I have written CodeIgniter routing, but I seem to remember the method names being case sensitive, so the Profile having a capital 'P' could be causing an issue?
The way I'd usually go about debugging this though is to try renaming the method, renaming the controller; the idea being you can figure out if the issue is in the routing, or if there's some unseen typo or other issue within the code, or whether its the route itself
Not sure if this helps? Good luck getting it resolved though!
Mac OSX is case sensitive, make sure the filenames in controllers match the case you are trying to access in your routes. Also it would help if you post the output too. I had a similar problem a while back which I solved by renaming my filenames in accordance to routes defined. Please refer to my answer HMVC codeigniter works on local server but not on web server here.
In the default controller you can not use sub folders if you are using CI 3 unless you have modified MY_Router.php
in application/core/MY_Router.php
This is the original Question here
<?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.');
}
}