简体   繁体   中英

how to create a Codeigniter route

I have a CodeIgniter website and the link structure for the inner pages look like below:

http://www.example.com/pages/Campus-Tour/18/3

pages is my controller name and campus-tour, 18 and 3 are the values passed into the index method.

I want to make a pretty url, so can I use something like this in routes.php?

$route['campus-life/campus-tour'] = 'pages/Campus-Tour/18/3';

to show this URL " http://www.example.com/campus-life/campus-tour "

Please use this syntax for routing.

$route['string-that-you-want-to-show-in-url'] = 'controller/function/arg1/arg2';

Example --

$route['campus-life/campus-tour'] = 'pages/campus_tour/$1/$2';

Note:- PHP doesn't support hyphen(-) inside function name, your function name (Campus-Tour) is invalid, please check it & use the above syntax.

FYI: A valid function name starts with a letter or underscore , followed by any number of letters, numbers, or underscores.


If you transmit data URL with this to pages/Campus-Tour/18/3 this campus-life/campus-tour it's clearly missing your URL parameter as you can see.

What you can do is <form> submit with POST and in the function, you can catch those.

In View

<form action ="campus-life/campus-tour" method ="post">
    <input type="hidden" name="first_param" value = "18"/>
    <input type="hidden" name="second_param" value = "3"/>
    <input type="submit"> # make this a No button. Should looks like <a> tag. Use CSS. (check Link)
</form>

In Controller

function Name() #add valid function/controller name
{
    # catch the POST values
}

Reference : How to make a button look like a link?

Following is the syntax for routing. Please use this:

$route['pages/Campus-Tour/(:any)/(:any)'] = 'pages/campus-tour/$1/$2';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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