codeigniter默认控制器url路由 [英] codeigniter default controller url routing

查看:147
本文介绍了codeigniter默认控制器url路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个简单的网站,并希望有类似于facebook的网址工作,所以通过键入domain.com/username我会得到一个用户配置文件。我可以这样做了。

I am putting together a simple networking site and would like to have the urls work similar to facebook so by typing in domain.com/username I would get a user profile. I can do this already this way

登录用户个人资料

domain.com/

其他用户个人资料

domain.com/home/username

控制器被称为home,我目前正在使用_remap函数来检查额外的url参数,以根据是否传递不同的数据。

The default controller is called home and I am currently using the _remap function to check for extra url parameters to display different data based on what is or isn't passed.

我的问题是我将如何映射,以便我可以访问用户配置文件:

My question is how would I map this out so I can access user profiles by:

domain.com/username 

有没有可以使用的routes.php设置? htaccess?

Is there a routes.php setting I could use? htaccess?

推荐答案

您应该在config / routes.php

you should add the following rule in your config/routes.php

// Profile Route
$route['(:any)'] = 'home/$1';

这会将您的所有请求重新路由到家庭控制器

因此 domain / michelle 将重新路由为 domain / home / michelle
,但在这种情况下,所有您的请求将被重新路由到家庭控制器
例如。 domain / pages / about 也将被重新路由到 domain / home / pages / about

so domain/michelle will re be rerouted as domain/home/michelle but in this case all your requests will be rerouted to the home controller eg. domain/pages/about will also be rerouted to domain/home/pages/about wich false.

domain/michelle => domain/home/michelle // TRUE
domain/pages/about => domain/home/pages/about // FALSE

因此您必须为每个请求配置文件路由之前的同一规则
eg。 $ route ['pages /(:any)'] ='pages / $ 1';

一起:

// Page Route 
$route['pages/(:any)']  = 'pages/$1'

// Last Profile Route
$route['(:any)'] = 'home/$1';

这篇关于codeigniter默认控制器url路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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