Zend Framework website.com/username [英] Zend Framework website.com/username

查看:34
本文介绍了Zend Framework website.com/username的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Zend Framework 开发的应用程序之一要求通过 website.com/username 访问用户的个人资料页面,而其他页面应通过 website.com/controller_name/action_name 访问

One of the application I am developing using Zend Framework requires the user's profile page to be accessed via website.com/username, while other pages should be accessed by website.com/controller_name/action_name

我不太确定如何实现这一点,但是,我觉得可以通过在 .htaccess 文件中进行一些调整来实现.

I am not too sure how can this be achieved, however, I feel this can be done with some tweaks in the .htaccess file.

这里有人可以帮我吗?

非常感谢

推荐答案

如前所述,您可以使用自定义路由来路由单级请求.但是,这也将覆盖默认路由.如果您使用模块,这将不再起作用 example.com/.

As suggested before, you can use a custom route that will route single level requests. However, this will also override the default route. If you're using modules, this will no longer work example.com/<module>.

我以前这样做过,但仅适用于静态页面.我想要这个:

I have done this before but only for static pages. I wanted this:

 example.com/about 

而不是这个:

example.com/<some-id>/about 

同时保持默认路由,所以这仍然有效

while maintaining the default route so this still works

example.com/<module>
example.com/<controller>

我这样做的方法是使用插件来测试我的请求是否可以被发送.如果无法使用默认路由发送请求,那么我会将请求更改为正确的模块以加载我的页面.这是一个示例插件:

The way I did this was using a plugin to test if my request could be dispatched. If the request could not be dispatched using the default route, then I would change the request to the proper module to load my page. Here is a sample plugin:

class My_Controller_Plugin_UsernameRoute extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();

        if (!$dispatcher->isDispatchable($request)) {

            $username = $request->getControllerName();

            $request->setModuleName('users');
            $request->setControllerName('dashboard');
            $request->setActionName('index');
            $request->setParam('username', $username);

            /** Prevents infinite loop if you make a mistake in the new request **/
            if ($dispatcher->isDispatchable($request)) {
                $request->setDispatched(false);
            }

        }

    }
}

这篇关于Zend Framework website.com/username的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆