【问题标题】:how to modify routes in codeigniter如何在codeigniter中修改路由
【发布时间】:2012-12-22 10:04:19
【问题描述】:

帮我解决这个问题。

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

如何让 Codeigniter 知道要查找 “本地主机/CI/storeurl”。

我有一个名为 index 的函数,它接受 Base.php 类中的参数 storeURL。帮我解决这个问题。提前致谢。

【问题讨论】:

标签: php codeigniter url routing


【解决方案1】:

我终于找到了我要找的东西。这是我的代码在 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】:

    只需三个步骤即可在 WAMP 环境中的 CodeIgniter 中从 URL 中删除 index.php

    1. 在CodeIgniter的根目录下创建.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']改成应用程序文件夹中config.php中的空字符串,方法如下:

      $config['index_page'] = '';
      
    3. httpd.conf 文件中启用 Apache 的 rewrite_module

    也可以参考this article

    【讨论】:

    • 我只是不想删除 index.php 文件。但类名,即基数(在上面的例子中)也是如此。再看看我说过的那些网址。可以在 routes.php 中修改吗?
    • 不,你不能用 codeigniter 做到这一点。你可以在核心 PHP 中使用 mod_rewrite 和 htaccess 来完成。
    【解决方案3】:

    这里有一些伪代码和一些正确方向的要点,可以帮助您入门。

    首先,您需要从您的网址中删除 index.php。这里肯定有数百个问题已经回答了这部分问题,所以我将这部分留给你。

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

    为此,将一条路由添加到routes.php 的底部作为全部内容。然后可以将其他控制器(新闻、事件等)的路由添加到此控制器之上。这是一个示例 routes.php 可以帮助您入门。

    config/routes.php

    // Other routes go up here
    
    // Catch all route 
    $route['(:any)'] = 'page/view';
    
    // Home page route 
    $route['default_controller'] = "page/index";
    

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

    controllers/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。

    以上都是从内存中输入的伪代码,所以它可能不会直接工作,但希望能给你一个线索,告诉你如何达到你想要的结果。

    【讨论】:

    • 谢谢 Jeemusu。我目前正在研究您在此处提到的相同想法。如果效果很好,将分享答案。你在这里应用的想法正是我现在的想法。我现在正在努力。
    • 很高兴它对您有所帮助,但刚开始使用 codeigniter 可能会很棘手。
    • 我有答案了 Jeemsu bro。我已经分享了代码。谢谢。
    【解决方案4】:

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

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

    路由会注意,只要$this->uri->segment(2)是方法,就会去执行Class的方法。你的网址会很漂亮:

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

    我还了解到路线必须具体。如果网址是这样的:

    base_url()/travel/singapore/listing/1201

    那么你的路线必须是:

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

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

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

    玩得开心:)

    【讨论】:

      猜你喜欢
      • 2017-01-18
      • 2012-08-11
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多