简体   繁体   English

Codeigniter路由问题

[英]Codeigniter routing issues

I am customizing my routes in codeigniter, here is a sample of my routes.php: 我正在codeigniter中自定义路线,这是我的route.php的示例:

$route['default_controller'] = 'maincontroller/main';                                                                                                                                                                                    
$route['404_override'] = '';     

$route['test'] = 'maincontroller/test';

here is the maincontroller class: 这是maincontroller类:

class Maincontroller extends CI_Controller  
{                 
    public function __construct( )
    {
        parent::__construct( ); 
        $this->load->library('session');  
        $this->init( ); 
    }


    public function main( )         
    {       
        echo "in main";
    } 

    public function test( )
    {
        echo "test";
    }
}

but when i access this url in my browser: /test, i get the server's 404 page not found error. 但是,当我在浏览器中访问此URL时:/ test,我得到服务器的404页面未找到错误。

I have no idea what im doing wrong, i followed the routes guide in codeigniter user guide. 我不知道我在做什么错,我遵循了Codeigniter用户指南中的路由指南。

Instead of going to /test , try going to /index.php/test 与其前往/ test,不如尝试前往/index.php/test

By default CodeIgniter runs everything through index.php. 默认情况下,CodeIgniter通过index.php运行所有内容。

You can change this behaviour by adding a .htaccess file to the site root folder. 您可以通过将.htaccess文件添加到站点根文件夹来更改此行为。

This is taken straight from the CodeIgniter user guide. 直接从CodeIgniter用户指南中获取。

https://ellislab.com/codeigniter/user-guide/general/urls.html https://ellislab.com/codeigniter/user-guide/general/urls.html

The htaccess file htaccess文件

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

If you do this you also need to change your config/config.php file 如果这样做,您还需要更改config / config.php文件

find

$config['index_page'] = 'index.php';

change it to 更改为

$config['index_page'] = '';

When you want to create links in a page (view) you can use the built in site_url() function, you may need to load the url helper for this. 当您想在页面(视图)中创建链接时,可以使用内置的site_url()函数,您可能需要为此加载URL帮助器。

for example 例如

<?php echo site_url('test'); ?>

This will take into account the $config['index_page'] setting and create the right url for your link. 这将考虑$ config ['index_page']设置并为您的链接创建正确的URL。

if you are trying to run your site from localhost use the below code.. adding a .htaccess file to the site root folder (eg: mysite). 如果您尝试从本地主机运行站点,请使用以下代码..将.htaccess文件添加到站点根文件夹(例如:mysite)。

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /root folder/index.php/$1 [L]

for the site hosted on the remote hosting server, use this code 对于远程托管服务器上托管的站点,请使用此代码

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM