在路由中使用连字符( - ),而不是斜杠(/)或下划线(_) [英] Use hyphen(-) instead of slash(/) or underscore( _ ) in Routes

查看:690
本文介绍了在路由中使用连字符( - ),而不是斜杠(/)或下划线(_)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Codeigniter 3.x,使用 routes.php 我想创建动态路由,例如我有一个类名 Class1



我想输出网址


mysite.com/Class1-学生搜索


但是使用连字符( - )无效


$ b b

如果我放斜线(/),它工作,

  $ route [' -Search'] =search; 

它返回


mysite.com/Class1/Student-Search


并使用下划线。

  $ route ['(:any)_Student-Search'] =search; 

返回


mysite.com/Class1_Student-Search


但我想使用连字符我把它,它会去404错误页面,我使用这四个解决方案,但不为我工作。

  $ route ['(:any)-Student-Search'] =search; 
$ route ['([a-zA-Z] +) - Student-Search'] =search;
$ route ['([a-zA-Z-0-9] +) - Student-Search'] =search;
$ route ['(。*) - Student-Search'] =search;

如果我硬编码路由中的值

  $ route ['Class1-Student-Search'] =search; 

然后它也工作

方案

您尝试创建一个动态路线,这在 codeigniter 中是不可能的,如果您看到以下codeigniter流程图,您就能理解我的意思。





也可以在codeigniter官方中看到这个图表官方



每请求首先转到那里,您不能使其动态


I'm Using Codeigniter 3.x , Using routes.php I want to create dynamic routes, for example I have a class name Class1.

I want output url

mysite.com/Class1-Student-Search

But using hyphen(-) is not working

If I put a slash(/), it works,

$route['(:any)/Student-Search']  = "search";

it returns

mysite.com/Class1/Student-Search

and using underscore (_) also work.

$route['(:any)_Student-Search']  = "search";

returns

mysite.com/Class1_Student-Search

But I want to use hyphen(-), if I put it, it will go to 404 error page, I used these four solutions but not work for me.

$route['(:any)-Student-Search']  = "search";
$route['([a-zA-Z]+)-Student-Search']  = "search";
$route['([a-zA-Z-0-9]+)-Student-Search']  = "search";
$route['(.*)-Student-Search']  = "search";

And if i hardcode the value in route

$route['Class1-Student-Search']  = "search";

Then it also working

解决方案

You trying to create a dynamic routes which is not possible in codeigniter if you see the following flow chart of codeigniter you understand what i mean.

also you can see this chart in codeigniter official website

when you try to redirect or call some url it's work like this

Every request first goes to route there for you can't make it dynamic

这篇关于在路由中使用连字符( - ),而不是斜杠(/)或下划线(_)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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