目前,在我的 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.
发布评论
评论(5)
试试这个:
Try this :
您问题的答案是您必须明确设置路线。
它如何知道给定函数位于哪个控制器???
你必须告诉它。
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.
使用 mod_rewrite (如果控制器始终同名)
use mod_rewrite (if the controller is always the same name)
好吧,我可以想到一种方法来做到这一点,但这可能比仅仅为每个函数编写路由更痛苦。
您需要使用
application/core/MY_Router.php
扩展Router.php
并覆盖_validate_request()
方法。这基本上决定了这是否是一条有效的路线。它会检查控制器类是否存在,如果不存在则失败。
您需要用一些假设没有控制器段的代码替换它,然后扫描每个控制器并检查它是否包含调用的方法(它将是段 1,因为没有控制器)。
现在是棘手的部分,在 CI 生命周期的这一点上,您的控制器显然尚未加载,因此您无法使用 method_exists() 还没有。
你需要一次加载一个控制器,然后每个控制器运行一次
,如果它是 true,则设置然后继续调用:
然后 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
withapplication/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
and if its true, then set then go ahead and call:
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.