我正在使用Zend Framework并创建基于个人资料的网站。当我想创建个人资料网址时,我陷入了困境。要求如下:
http[:]//abc.com/myprofileurl
我是zend的新手,我只知道在zend框架中,url规则是
http[:]//domain.com/controller/action/params
但我需要将配置文件名称放在控制器的位置。
所以,伙计们请帮我解决这个问题。我已经花了4个小时在互联网上搜索解决方案,但找不到任何东西。
先谢谢
答案 0 :(得分:0)
我建议您在网址中添加一些有辨别力的值,以避免规则冲突。 在示例中,我将显示所有“用户”相关的URL位于前缀“user”下,因此我们可以拥有用户/登录,用户/注销,用户/注册,用户/更改密码等(顺便说一下使用相同的逻辑)在zfcuser模块中。)
我们将创建一个像/ user / profile /:user这样的网址,其中:user部分是网址的动态部分。
我假设您正在使用Application模块并且您已经创建并注册了UserController(如果名称不同,下面的代码将需要更改)
'router' => array(
'routes' => array(
// other routes configs here
'user' => array(
'type' => 'Literal',
'priority' => 1000,
'options' => array(
'route' => '/user',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'profile' => array(
'type' => 'Segment',
'options' => array(
'route' => '/profile/:user',
'defaults' => array(
'action' => 'profile',
),
),
),
),
),
使用此路由配置,您的应用程序将在调用url / user时使用UserController :: index进行响应。 当调用/ user / profile / someuser时,将使用UserController :: profile进行响应。
请注意,someuser部分是必需的,因为我们在规则中这样说。为了使其不是强制性的,我们应该编写如下的规则
'route' => '/profile/[:user]',
在UserController的个人资料操作中,您可以使用:user值,如关注
$this->params()->fromRoute('user')
这只是您可以采用的解决方案之一,例如您可以编写没有子路由的规则,例如关注
'user' => array(
'type' => 'Segment',
'options' => array(
'route' => '/user/profile/:user',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'User',
'action' => 'profile',
),
),
),
您可以选择符合您需求的解决方案。 我建议你也阅读ZF2 official routing documentation。
答案 1 :(得分:0)
我通过自我实验得到了解决方案。
我创建了一个名为“User”的模块,然后创建了一个名为“Profile”的控制器,并在用户模块中的module.config.php中添加路由,如下所示:
'router' => array(
'routes' => array(
// other routes configs here
'profile' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:username',
'constraints' => array(
'username' => '[\w-.]+',
),
'defaults' => array(
'__NAMESPACE__' => 'User\Controller',
'controller' => 'Profile',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
),
),
),
),
这正是我想要的:P
http[:]//mydomain.com/myusername
谢谢你们!