代码点火器路由

发布于 2024-10-13 15:00:18 字数 359 浏览 2 评论 0原文

我目前正在为客户开发 CMS,我将使用 Codeigniter 进行构建,这只是一个快速项目,所以我并不是在寻找强大的解决方案。

为了创建页面,我要保存页面详细信息,并根据与 mysql 表中的 slug 匹配的 slug 提取正确的页面。

然而,我的问题是,要使其工作,我必须将这个 slug 从控制器的 URL 传递到模型,这意味着我在 URL 中也有我不希望的控制器,是否可以删除来自带有路由的 URL 的控制器?

所以

/页面/我们的故事

变成

/我们的故事

这可能吗

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.

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.

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?

so

/page/our-story

becomes

/our-story

Is this possible

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雾里花 2024-10-20 15:00:18

我建议这样做。

假设您有:控制器“页面”/方法“显示”

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

或方法是我不推荐的索引,如果您有新闻之类的内容,请添加以下内容。

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

就是这样。

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.

仄言 2024-10-20 15:00:18

是的,当然。我最近刚刚自己构建了一个 Codeigniter 驱动的 CMS。路由的全部目的是改变 URL 的外观和功能。它可以帮助您摆脱控制器/函数/参数/参数范式,并让您选择您想要的 url 的外观。

  1. 在控制器目录中创建一个页面控制器
  2. 在其中放置一个_remap函数以捕获对控制器的所有请求
  3. 如果您使用的是Bitbucket的最新版本的CI 2.0,那么在您的routes.php文件中,您可以将其放在底部文件: $routes['404_override'] = "pages"; 然后所有对不存在的控制器的调用都将发送到您的控制器,然后您可以检查 URL 块是否存在。您还应该将页面设置为默认控制器值。

请参阅我几个月来对类似问题的回答返回我在 Codeigniter CMS 中使用的示例代码和工作代码。

Yes, certainly. I just recently built a Codeigniter driven CMS myself. 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.

  1. Create a pages controller in your controllers directory
  2. Place a _remap function inside of it to catch all requests to the controller
  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"; 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. 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.

等你爱我 2024-10-20 15:00:18

这是我在最近的项目中用于实现此目的的代码。我从什么地方借来的;不记得在哪里了。

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);
}

然后,我的 index 函数就是您放置 page 函数的位置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文