从 CodeIgniter 中的 URL 中删除控制器名称

发布于 2024-11-01 16:02:22 字数 393 浏览 2 评论 0 原文

目前,在我的 CI 项目中,我有一个控制器来处理帐户的所有事务。例如注册、登录、激活等。

我的路由是这样工作的...

domain.com/account/login/domain.com/account/register/

如何从路由中删除 account ,同时还要从其他页面中删除控制器。

我基本上希望控制器始终被删除。我这样做的原因之一是搜索引擎优化,搜索引擎根据页面在网站中的深度对页面的重要性进行排名。

我似乎实现这一目标的唯一方法是对每个页面执行诸如 route['activate'] = 'account/activate'; 之类的操作,这将是一个巨大的麻烦。

Currently in my CI project I have a single controller that handles all things account. Such-as register, login, activation, etc.

My routes work as such...

domain.com/account/login/ or domain.com/account/register/

How can I remove account from the route while also being about to remove the controller from other pages.

I basically want the controller to always be removed. One of my reasons for this is SEO, search engine rank the importunateness of a page based on how deep it is in a website.

The only way I have seem to achieve this is to do some thing like route['activate'] = 'account/activate'; for every single page, which would be a huge hassle.

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

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

发布评论

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

评论(5

南七夏 2024-11-08 16:02:22
$route['^(?!other|controllers).*'] = “account/$0″;
$route['^(?!other|controllers).*'] = “account/$0″;
几度春秋 2024-11-08 16:02:22

试试这个:

$route['(:any)'] = "account/$1";

Try this :

$route['(:any)'] = "account/$1";
记忆之渊 2024-11-08 16:02:22

您问题的答案是您必须明确设置路线。

它如何知道给定函数位于哪个控制器???

你必须告诉它。

The answer to your question is that you DO have to explicitly set the routes.

How is it going to know which controller a given function is in????

You have to tell it.

┈┾☆殇 2024-11-08 16:02:22

使用 mod_rewrite (如果控制器始终同名)

use mod_rewrite (if the controller is always the same name)

椒妓 2024-11-08 16:02:22

好吧,我可以想到一种方法来做到这一点,但这可能比仅仅为每个函数编写路由更痛苦。

您需要使用 application/core/MY_Router.php 扩展 Router.php 并覆盖 _validate_request() 方法。这基本上决定了这是否是一条有效的路线。

它会检查控制器类是否存在,如果不存在则失败。
您需要用一些假设没有控制器段的代码替换它,然后扫描每个控制器并检查它是否包含调用的方法(它将是段 1,因为没有控制器)。

现在是棘手的部分,在 CI 生命周期的这一点上,您的控制器显然尚未加载,因此您无法使用 method_exists() 还没有。

你需要一次加载一个控制器,然后每个控制器运行一次

method_exists($loaded_class, $method_name)

,如果它是 true,则设置然后继续调用:

$this->set_class('the_name_of_the_scanned_class_which_had_the_method');

然后 CI 可以继续正常运行,它将加载你的方法,而用户不知道什么它加载的控制器。

..恕我直言,可能不值得这么麻烦。一种更简单的解决方案是只有一个控制器和一条通往该控制器的路由。

Ok, I can think of one way to do this, but it is probably gonna be more of a pain than just writing out routes for each function.

You need to extend the Router.php with application/core/MY_Router.php and overide the _validate_request() method. Which basically decides if this this is a valid route or not.

it does a check to see if the controller class exists then fails if it doesn't exist.
You need to replace this with some code which assumes no controller segment, then scans thru each of your controllers and checks if it contains the method called (it will be segment 1, since theres no controller).

Now the tricky part, at this point in the CI lifecycle your controller obviously isnt loaded, so you cant examine it using method_exists() yet.

You need to load your controllers one at a time, and then for each one run

method_exists($loaded_class, $method_name)

and if its true, then set then go ahead and call:

$this->set_class('the_name_of_the_scanned_class_which_had_the_method');

Then CI can keep going on as normal and it will load your methods without the user ever know what controller it loaded from.

.. probably not worth the hassle imho. A much easier solution would be to just have one controller and one route to that controller.

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