简体   繁体   English

代码点火器路由

[英]codeigniter routing

I am currently working on CMS for a client, and I am going to be using Codeigniter to build on top of, it is only a quick project so I am not looking for a robust solution. 我目前正在为客户端开发CMS,并且我将使用Codeigniter进行构建,因为这只是一个快速的项目,所以我没有在寻找可靠的解决方案。

To create pages, I am getting to save the page details and the pull the correct page, based on the slug matching the slug in the mysql table. 要创建页面,我要保存页面详细信息,并根据与mysql表中的段匹配的段来提取正确的页面。

My question is however, for this to work, I have to pass this slug from the URL the controller then to the model, this means that I also have too have the controller in the URL which I do not want is it possible to remove the controller from the URL with routes? 但是,我的问题是,要使其正常工作,我必须先从控制器的URL中传递该段信息,然后再传递给模型,这意味着我也希望在URL中也具有该控制器,但我希望将其删除控制器从带有路由的URL?

so 所以

/page/our-story / page /我们的故事

becomes 变成

/our-story /我们的故事

Is this possible 这可能吗

I would recommend to do it this way. 我建议这样做。

Let's say that you have : controller "page" / Method "show" 假设您有:控制器“页面” /方法“显示”

$route['page/show/:any'] = "$1";

or method is index which I don't recommend, and if you have something like news, add the following. 或方法是我不推荐的索引,如果您有类似新闻的内容,请添加以下内容。

$route['news/show/:any'] = "news/$1";

That's it. 而已。

Yes, certainly. 是的,当然了。 I just recently built a Codeigniter driven CMS myself. 我最近刚刚自己构建了一个Codeigniter驱动的CMS。 The whole purpose of routes is to change how your urls look and function. 路由的全部目的是更改您的网址的外观和功能。 It helps you break away from the controller/function/argument/argument paradigm and lets you choose how you want your url's to look like. 它可以帮助您脱离控制器/函数/参数/参数范式,并可以选择想要的URL外观。

  1. Create a pages controller in your controllers directory 在controllers目录中创建页面控制器
  2. Place a _remap function inside of it to catch all requests to the controller 在其中放置_remap函数以捕获对控制器的所有请求
  3. If you are using the latest version of CI 2.0 from Bitbucket, then in your routes.php file you can put this at the bottom of the file: $routes['404_override'] = "pages"; 如果您正在使用Bitbucket的CI 2.0的最新版本,则可以在$routes['404_override'] = "pages";文件中将该文件放在文件底部: $routes['404_override'] = "pages"; and then all calls to controllers that don't exist will be sent to your controller and you then can check for the presence of URL chunks. 然后所有不存在的对控制器的调用都将发送到您的控制器,然后您可以检查是否存在URL块。 You should also make pages your default controller value as well. 您还应该将页面设为默认控制器值。

See my answer for a similar question here from a few months back for example code and working code that I use in my Codeigniter CMS. 几个月前,我在这里针对类似问题看到了我的答案,例如我在Codeigniter CMS中使用的示例代码和工作代码。

Here's the code I used in a recent project to achieve this. 这是我在最近的项目中用于实现此目的的代码。 I borrowed it from somewhere; 我从某处借来的。 can't remember where. 不记得在哪里。

function _remap($method)
{
  $param_offset = 2;

  // Default to index
  if ( ! method_exists($this, $method))
  {
    // We need one more param
    $param_offset = 1;
    $method = 'index';
  }

  // Since all we get is $method, load up everything else in the URI
  $params = array_slice($this->uri->rsegment_array(), $param_offset);

  // Call the determined method with all params
  call_user_func_array(array($this, $method), $params);
}

Then, my index function is where you would put your page function. 然后,在我的index函数中放置page函数。

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

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