简体   繁体   English

如何创建Codeigniter路线

[英]how to create a Codeigniter route

I have a CodeIgniter website and the link structure for the inner pages look like below: 我有一个CodeIgniter网站,内页的链接结构如下所示:

http://www.example.com/pages/Campus-Tour/18/3 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. pages是我的控制器名称,campus-tour是18和3是传递到index方法中的值。

I want to make a pretty url, so can I use something like this in routes.php? 我想创建一个漂亮的网址,因此可以在routes.php中使用类似的内容吗?

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

to show this URL " http://www.example.com/campus-life/campus-tour " 显示此网址“ 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. 注意:-PHP在函数名称中不支持连字符(-),您的函数名称(Campus-Tour)无效,请检查它并使用上面的语法。

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. 如果您将与此相关的数据URL传输到pages/Campus-Tour/18/3campus-life/campus-tour则显然会丢失您的URL参数。

What you can do is <form> submit with POST and in the function, you can catch those. 您可以做的是<form>POST提交,在函数中,您可以捕获这些内容。

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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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