php - Use hyphen(-) instead of slash(/) or underscore( _ ) in Routes


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

I want output url

But using hyphen(-) is not working

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

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

it returns

and using underscore (_) also work.

$route['(:any)_Student-Search']  = "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.

enter image description here

also you can see this chart in codeigniter official website

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

enter image description here

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




Here is my solution, it is working for me, do like this.

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

then in your link button, hopefully in your view, href the link like this.

href="/<?php echo $row->classname; ?>-Student-Search"

the point is that not only you have to make your routes, also suffix it in your href link also the same way.

People are also looking for solutions to the problem: php - Pass value while submitting a form button from model to controller


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.