需要设置更清晰的链接 - 当前链接太长 - Zend 框架

发布于 2024-08-22 01:28:46 字数 528 浏览 6 评论 0原文

我正在为我的项目使用 zend 框架 - 我尝试使用干净的链接进行设置 - 但是我注意到我的链接似乎变得非常非常长,想知道此时是否有一种方法可以合并我可以使用较小链接的结构。

下面是一个例子,我的网站有一个目录控制器,它有一个带有 id 参数的人员操作 - 因此,查看某个 id 的人员的链接如下所示:

www.mywebsite.com/directory/person/id/1809

看起来不错,但我现在正在努力使链接更具可读性和搜索引擎友好 - 我想要类似的东西 - 假设用户 ID 1809 名为 John Smith:

www.mywebsite.com/directory/person/John-Smith-1809

像下面这样的非常基本的东西会更好:

www.mywebsite.com/John-Smith-1809

虽然我不知道如何在不提及控制器或的情况下管理它行动...任何想法都会真正有帮助...

I'm using the zend framework for my project - I've tried to set up using clean links - however I've noticed that my links seem to be getting really really long and wonder if at this point there would be a way to incorporate a structure where I could user smaller links.

Heres an example my website has a directory controller, which has a person action which takes an id parameter - so then a link to see a person of a certin id looks like:

www.mywebsite.com/directory/person/id/1809

It seems ok but I'm working now to make links more readable and search engine friendly - I would like to have something like - assuming the user ID 1809 is named John Smith:

www.mywebsite.com/directory/person/John-Smith-1809

Something really basic like the following would be even better:

www.mywebsite.com/John-Smith-1809

Although I don't know how I could manage that without mentioning a controller or action...any ideas guys would really help...

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

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

发布评论

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

评论(3

静赏你的温柔 2024-08-29 01:28:47

看看 ZF关于如何使用路由的手册

基本上,您需要这样的东西才能使其工作:

$route = new Zend_Controller_Router_Route_Regex(
    '(.+)-(\d+)',
    array(
        'controller' => 'directory',
        'action'     => 'person'
    ),
    array(
        1 => 'name',
        2 => 'id'
    ),
    '%s-%d'
);
$router->addRoute('directoryPerson', $route);

注意:不要指望它开箱即用。您可能需要修改它。不过应该让你进入正确的方向。

Have a look at the ZF Manual on how to use Routes.

Basically, you need something like this to make it work:

$route = new Zend_Controller_Router_Route_Regex(
    '(.+)-(\d+)',
    array(
        'controller' => 'directory',
        'action'     => 'person'
    ),
    array(
        1 => 'name',
        2 => 'id'
    ),
    '%s-%d'
);
$router->addRoute('directoryPerson', $route);

Note: don't expect this to run out of the box. You will likely have to modify it. Should get you into the right direction though.

拧巴小姐 2024-08-29 01:28:47

就像之前的答案所说,您可以使用正则表达式路线,但对于初学者来说,使用“常规”路线可能会更容易,如下所示:

http://yoursite.com/1234/John-Smith/

在你的引导程序中,你会做:

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();

$router->addRoute('user', new Zend_Controller_Router_Route(
    ':user-id/:user-name/',
    array(
        'module' => 'user',
        'controller' => 'user',
        'action' => 'read'
    )
));

这会向路由器添加一个名为“user”的路由,其形式为/用户id/用户名,所以/1234/John-Smith/。它将在用户模块中查找用户控制器,然后执行读取操作。如果您愿意,您当然可以将其更改为另一个模块/控制器/操作。

然后,您将在 User 模块中拥有 User_UserController,该模块具有 readAction();在该操作中,您可以通过以下方式获取 id:

$userId = $this->_getParam('user-id');

类似地,如果您需要它,您可以检索名称。

$userName = $this->_getParam('user-name');

在视图脚本中,您可以使用以下命令生成该路由的 url:

<?php echo $this->url(array('user-id' => 1234, 'user-name' => 'John Smith'), 'user', true); ?>

注意“user”参数,它告诉路由器使用我们之前定义的“user”路由。

Like a previous answer said you could use a Regex route, but for starters it would probably be easier to use a "regular" route that looks like:

http://yoursite.com/1234/John-Smith/

In your bootstrap you'd do:

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();

$router->addRoute('user', new Zend_Controller_Router_Route(
    ':user-id/:user-name/',
    array(
        'module' => 'user',
        'controller' => 'user',
        'action' => 'read'
    )
));

This adds a route called "user" to the router, that has the form /user-id/user-name, so /1234/John-Smith/. It will look for a usercontroller in a user module, and go to the read action. You can of course change that to another module/controller/action if you wish.

You'd then have that User_UserController in the User module, that has a readAction(); In that action you could get the id with:

$userId = $this->_getParam('user-id');

Similary, should you need it, you could retrieve the name.

$userName = $this->_getParam('user-name');

In a view script you could generate an url to that route with:

<?php echo $this->url(array('user-id' => 1234, 'user-name' => 'John Smith'), 'user', true); ?>

Note the 'user' parameter, which tells the router to use the "user" route we defined earlier.

甜柠檬 2024-08-29 01:28:47

您可以使用 apache 模块 mod_rewrite 重写 URL 的

http://httpd.apache .org/docs/1.3/mod/mod_rewrite.html

<IfModule mod_rewrite.c>

  RewriteEngine On

  RewriteCond ###blah blah blah###
  RewriteRule ###blah blah blah###

</IfModule>

You can use the apache module mod_rewrite to rewrite your URL's

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

<IfModule mod_rewrite.c>

  RewriteEngine On

  RewriteCond ###blah blah blah###
  RewriteRule ###blah blah blah###

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