简体   繁体   English

在Codeigniter URL route.php中出现问题

[英]Getting issue in codeigniter URL routes.php

I'm working on a project where I need to create clean URL for every products, see following pattern:- 我正在一个项目中,我需要为每个产品创建干净的URL,请参见以下模式:-

example.com/Single_Product_Page example.com/Single_Product_Page

The original URL is example.com/browse/Single_Product_Page and I used following code:- 原始网址为example.com/browse/Single_Product_Page ,我使用了以下代码:-

$route['(:any)'] = 'Products/browse/$1'; 

I've two page (1) example.com/Product 我有两页(1)example.com/Product
and (2) example.com/Products/Single_Product_Page (2)example.com/Products/Single_Product_Page

And it is working fine but now I'm unable to open Products page and when I tried to open it, It open Single_Product_Page 它工作正常,但是现在我无法打开“ 产品”页面,当我尝试打开它时,它会打开Single_Product_Page

Please help me to solve this issue. 请帮我解决这个问题。

You need to update your routes similar to this example (which works OK on my site): 您需要更新类似于以下示例的路由(在我的网站上可以正常运行):

$route['products'] = 'controller/myfunction/argument';
$route['products/:any'] = 'controller/myfunction/another_argument/$1/1';

you can get more insight from the docs here 您可以从此处的文档中获得更多见解

You can use a little hack to use just your controller name (and that's a must of course) but eliminate the need to write the method name and pass your parameters directly after the controller name so basically your url would look like this: http://localhost/controller/parameter and that would give you shorter urls as you intend but not an SEO friendly as you claimed. 您可以使用一些技巧来仅使用您的控制器名称(当然这是必须的),但是无需编写方法名称并在控制器名称之后直接传递参数,因此基本上您的url看起来像这样: http://localhost/controller/parameter ,这将为您提供较短的网址,而不是您声称的SEO友好目录。

You can use _remap in your controller and check if it matched a method process it normally or pass it to your index (which is your default method that's doesn't have to be written in your url) .. and now you won't need to use index in your url as you intended. 您可以在控制器中使用_remap并检查它是否与正常处理的方法相匹配或将其传递给索引(这是您不必在url中编写的默认方法)..现在您不需要按照预期在网址中使用索引。

public function _remap($method)
{
    if ($method === 'some_method_in_your_controller')
    {
            $this->$method();
    }
    else
    {
            $this->index($method);
    }
}

Or you can depend on ajax for all of your crud operations and your url will be pretty much fixed all the time. 或者,您可以依赖ajax进行所有操作,并且url几乎总是固定的。

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

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