本文介绍了如何将CodeIgniter网址转换为用户友好的网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了很多与此有关的问题和文章,但找不到如何完成此工作。

I've explored lot of questions and articles regarding this but I can't find how to get this done.

我正在做一个提供规格的网站几种产品,例如手机,平板电脑,电视等。这是我的工作:

控制器-规格(创建和显示所有产品的规格)

方法-展示(获取详细信息

方法-索引(列出存储在表中的所有模型的名称。这是我建立锚链接的位置)

I'm doing a website which provides specifications of several products such as phones, tablets, tv etc. Here's what I've:
Controller - Specs (create and display specification of all products)
Method - Display (fetches detailed specs of selected model and shows)
Method - Index (lists names of all models stored in the table. this is where I build anchor links)

显示方法带有三个参数(1、2、3)。

1-产品类型(电话,平板电脑,电视等)

2-型号Slug(iphone-6, galaxy-tab-s3,bravia-kdl-50w800d等)

3-型号ID(1、4、13等)

Display method takes three arguments (1, 2, 3).
1 - Type of product (Phones, Tablets, TV etc)
2 - Model Slug (iphone-6, galaxy-tab-s3, bravia-kdl-50w800d etc)
3 - Model ID (1, 4, 13 etc)

我的网址现在是这样的:

localhost / sitename / specs / display / phones / iphone-6 / 1

localhost / sitename / specs / display / tablets / galaxy-tab-s3 / 4

localhost / sitename / specs / display / tv / bravia-kdl-50w800d / 13

My URLs right now are like this:
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13

我要实现的是这样的URL:

localhost / sitename / iphone-6

localhost / sitename / galaxy-tab-s3

localhost / sitename / bravia-kdl-50w800d

What I want to achieve is URLs which are like this:
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d

我不在乎重新构造我的表/控制器/方法或任何其他方法(如果可以使用任何方法来实现)。

I don't mind restructuring my tables/controllers/methods or anything else if this can be achieved using whatever.

感谢阅读。

编辑:

Route.php


Route.php

$route['default_controller'] = 'Specs/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;

这就是我构建锚链接的方式(view_file-> index.php,从索引调用方法):

This is how I'm building the anchor links (view_file->index.php, called from Index method):

<?php 
    foreach model(in the table)
        echo anchor(specs_controller.display_function.product_type.model_slug.model_id, model_name);
    end foreach
?>


推荐答案

请使用下面的路由代码来实现它。

Please use below routing code, to achieve it.

localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13

localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d

$route['(:any)'] = 'specs/display/$1/$1/$1';

让我知道您是否需要任何帮助。

Let me know if you need any help.

这篇关于如何将CodeIgniter网址转换为用户友好的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 04:44