如何修改codeigniter中的路由

时间:2013-01-08 14:18:18

标签: php codeigniter url routing

帮助我。

举个例子:我有这个正常的网址 “本地主机/ CI / index.php的/碱/ storeurl”。

我怎样才能让Codeigniter知道寻找 “本地主机/ CI / storeurl”。

我有一个名为index的函数,它接受Base.php类中的参数storeURL。帮助我。提前谢谢。

4 个答案:

答案 0 :(得分:3)

在WAMP环境中,只需要三个步骤就可以从CodeIgniter中的网址中删除index.php

1)与应用程序持有者并行创建.htaccess文件  并只复制以下代码:

RewriteEngine On
RewriteBase /CodeIgniter/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

2)将应用程序文件夹中$config['index_page']的{​​{1}}更改为空白,如下所示:

config.php

3)启用apache的$config['index_page'] = '';

您还可以参考http://goo.gl/3jfW8f

答案 1 :(得分:3)

我终于找到了我想要的东西。以下是我的代码在routes.php中的样子。

/* Custom Routes. */
// Store Normal Pages.
$route['home/(:any)'] = "base/home/$1";
$route['about/(:any)'] = "base/about/$1";
$route['services/(:any)'] = "base/services/$1";
$route['contact/(:any)'] = "base/contact/$1";
$route['feedback/(:any)'] = "base/feedback/$1";

// CategoryPage.
$route['category/(:any)/(:num)'] = "base/category/$1/$2";
// ProductPage.
$route['product/(:any)/(:num)'] = "base/product/$1/$2";


// For Main Home Page.
$route['(:any)'] = "base/home/$1";

我非常感谢所有帮助我解决这个问题的人。感谢你们。

答案 2 :(得分:1)

这是一些伪代码以及正确方向的几个要点,以帮助您入门。

首先,您需要从网址中删除index.php。这里必须有数百个问题已回答这部分问题,所以我会把这部分留给你。

正如许多人所评论的那样,没有办法在盒子外面实现这个url结构,并且需要相当多的代码才能工作。我实现这个特定设置的方式是将所有请求路由到单个控制器,并处理该控制器内的自定义路由。

为此,请在routes.php的底部添加一条路线作为全部捕捉。然后可以在此上面添加其他控制器(新闻,事件等)的路由。这是一个示例routes.php,可以帮助您入门。

<强>配置/ routes.php文件

// Other routes go up here

// Catch all route 
$route['(:any)'] = 'page/view';

// Home page route 
$route['default_controller'] = "page/index";

然后,您可以在view中创建一个controllers/page.php函数,用于检查网址(使用codeigniters url helper),并加载与该网址对应的正确视图。

<强>控制器/ page.php文件

class Page extends CI_Controller {

   public function index()
   {    
      // Do all code for your home page in here
   }

   public function view()
   {        
      // Retrive the url string
      $url = $this->uri->uri_string();

      // Check if the corresponding view file exists
      if (file_exists(APPPATH.'views/'.$url.'/index.php')){

         // Load the view
         $this->load->view($url.'/index');

      } else {
         show_404();
      } 
   }
}

例如,用户转到http://yoursite.com/CI/about-us,页面控制器视图函数将从URL中获取字符串about-us并将其设置为$ url变量,然后搜索您的文件结构检查相应的视图文件是否存在/application/views/about-us/index.php。如果是,则会加载该视图文件,如果它没有重定向到404.

以上是从内存中输入的所有伪代码,所以它可能不会直接工作,但希望能为您提供如何实现所需结果的线索。

答案 3 :(得分:0)

我总是无法让路由工作。 最后我找到了解决方案。我只是想分享以帮助有需要的人,因为我非常喜欢codeigniter,因为它快速而轻盈。 我正在研究动态类别标题,以便在$ this-&gt; uri-&gt; segment(1)上显示。

$route['(:any)/method/(:any)']  = 'Class/method';    
$route['(:any)/gallery/(:any)'] = 'Class/gallery';    
$route['(:any)/listing/(:any)'] = 'Class/listing';

路线会注意到,只要$ this-&gt; uri-&gt; segment(2)是方法,它就会执行Class的方法。你的网址很漂亮:

base_url()/business-services/gallery/6 
base_url()/travel/gallery/12

我还了解到路线必须具体。如果网址如下:

BASE_URL()/行程/新加坡/列表/ 1201

然后你的路线必须是:

$route['(:any)/(:any)/listing/(:num)']  = 'Class/listing';

如果你有传入方法的参数(取决于你是否使用param或uri_segment),

$route['(:any)/(:any)/listing/(:num)']  = 'Class/listing/$1';

玩得开心:)