简体   繁体   English

如何在使用Codeigniter配置路由时检查Controller是否已存在?

[英]How to check if Controller already exists when already configured route using Codeigniter?

On my website, I am loading the content dynamically from database like this 在我的网站上,我正在从这样的数据库中动态加载内容

e.g mysite.com/about-us   

for this, there is an enrtry in database, so it will load the content for 'about-us' & print it using "page" controller only. 为此,数据库中有一个enrtry,因此它将加载'about-us'的内容并仅使用“page”控制器打印它。

for this what I have done is, I have added below configuration in routes.php 为此,我所做的是,我在routes.php中添加了以下配置

$route[':any'] = "page";

but lets say if I already have controller named "about-us" and I want to load that & not the one from database, how can I do that? 但是,如果我已经拥有名为“about-us”的控制器并且我想加载那个而不是数据库中的那个,我怎么能这样做呢?

A smooth solution would be to use the error/missing_page controller and point it in the config/routes.php . 一个流畅的解决方案是使用error / missing_page控制器并将其指向config/routes.php

Then it would automaticly pick all existing controllers first, and then that controller. 然后它会首先自动选择所有现有的控制器,然后是该控制器。

You can also call show_404() if you don't find a record in the database. 如果在数据库中找不到记录,也可以调用show_404()

This allows you to create new controllers without having to point all of them in the route file. 这允许您创建新的控制器,而无需将所有控制器都指向路径文件。

Read about 404 override here 在这里阅读404覆盖

you need to add this 你需要添加这个

$route['about-us'] = "aboutus";
$route['about-us/(:any)'] = "aboutus/$1";

before 之前

$route[':any'] = "page";

as the CI route is not greedy, it will not check for the page controller after it finds the about-us controller. 由于CI路由不贪心,它在找到about-us控制器后不会检查页面控制器。

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

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