代码信号路由问题 [英] Codeigniter routing issues

查看:106
本文介绍了代码信号路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在codeigniter中定制我的路由,这里是我的routes.php示例:

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

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

这里是主控类:

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


public function main()
{
echoin main;
}

public function test()
{
echotest;
}
}

但是当我在我的浏览器中访问这个url:测试,我得到服务器的404页面未找到错误。



我不知道我做错了什么,我按照codeigniter用户指南中的路线指南。

解决方案

默认情况下,CodeIgniter通过index.php运行所有内容,而不是进入/ test,请尝试转到/index.php/test


$ b。



您可以通过将.htaccess文件添加到网站根文件夹来更改此行为。



直接从CodeIgniter用户指南。



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



htaccess文件

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

如果您这样做,更改config / config.php文件



查找

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

更改为

  $ config ['index_page'] =''; 

当您想在页面(视图)中创建链接时,可以使用内置的site_url函数,您可能需要为此加载url帮助器。



例如

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

这将考虑$ config ['index_page']设置,并为您的链接。


I am customizing my routes in codeigniter, here is a sample of my routes.php:

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

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

here is the maincontroller class:

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.

I have no idea what im doing wrong, i followed the routes guide in codeigniter user guide.

解决方案

Instead of going to /test , try going to /index.php/test

By default CodeIgniter runs everything through index.php.

You can change this behaviour by adding a .htaccess file to the site root folder.

This is taken straight from the CodeIgniter user guide.

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

The htaccess file

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

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.

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.

这篇关于代码信号路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆