我正在探索codeigniter。在应用程序启动时,默认控制器更改为加载我的控制器。
控制器正确加载视图,这很好,所以我猜路由按预期工作,但当我使用(手动在地址栏上输入同一控制器上的其他方法)相同的url模式/控制器/方法我得到404错误,视图存在。
是否必须更改一些默认路由行为或其他问题?
谢谢
我不知道您是否已经从url模式中删除了index.php,假设是这种情况,您应该在浏览器地址字段index.php/controller/method
中键入。(如果你手动输入你描述的url)
另一方面,如果你不想在每个链接上使用index.php,你可以考虑删除它,更多信息在这里
这可能是因为上面提到的index.php文件,或者如果你想摆脱index.php,请在你的应用程序中包含。htaccess文件。
config/config.php - modifiy
$config['base_url'] = 'index.php'
$config['base_url'] = '' // set it to blank
对于。htaccess文件,请参考以下代码
RewriteEngine on
RewriteCond $1 !^(index'.php|images|robots'.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
跟随这个
root_folder/. htaccess
删除url
中的index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
root_folder/应用程序/config/config . php
| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!
|
| If you need to allow multiple domains, remember that this file is still
| a PHP script and you can easily do that on your own.
|
*/
$config['base_url'] = 'http://[::1]/my-project/';
删除url中的
index.php
,即使在表单root_folder/应用程序/config/config . php
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';
s设置默认控制器,我的是'home'
root_folder/应用程序/配置/routes.php
| controller and method URI segments.
|
| Examples: my-controller/index -> my_controller/index
| my-controller/my-method -> my_controller/my_method
*/
$route['default_controller'] = 'home';
之后,确保所有控制器文件名大写。也是类名。
这在你需要实时上传时也很重要服务器。
root_folder/应用程序/控制器/home。
<?php
/**
*
*
* @author Lloric Garcia <emorickfighter@gmail.com>
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends MY_Controller {
public function index() {
}
}
那么这将是你的url
http://[::1]/my-project/home
https://www.codeigniter.com/userguide3/index.html这是我的设置,即使在实时服务器
所有这些都来自