繁体   English   中英

Phalcon PHP-如何自定义Route以使URL像wordpress一样友好

[英]Phalcon PHP - how to custom Route to make url friendly like wordpress

我想让我的网址友好,例如:

http://abc.com/I-want-to-make-my-url-friendly-like

http://abc.com/my-category/I-want-to-make-my-url-friendly-like

在Phalcon。

非常感谢你。

您可以在依赖自定义功能的路线上使用转化

// The action name allows dashes, 
// an action can be: /products/new-ipod-nano-4-generation
$router
  ->add(
    '/{category:[[a-z\-]+]/{slug:[a-z\-]+}', 
    array(
        'controller' => 'products', // This can be any controller you want
        'action'     => 'show' // Same here
    )
  )
  ->convert(
    'slug', 
    function ($slug) {
        return str_replace('-', '', $slug);
    }
  )
  ->convert(
    'category', 
    function ($category) {
        return str_replace('-', '', $category);
    }
  );

感谢Nikolaos Dimopoulos,您的回复意味着您会将slug转换为有效函数。 我找到了问题的答案(在我的项目中有3个等级类别):

   // Category
   $router->add(
        '/[a-z0-9-]{3,}/',
        array(
            'controller' => 'category',
            'action'     => 'index'
        )
    );
    $router->add(
        '/[a-z0-9-]{3,}/[a-z0-9-]{3,}/',
        array(
            'controller' => 'category',
            'action'     => 'index'
        )
    );
    $router->add(
        '/[a-z0-9-]{3,}/[a-z0-9-]{3,}/[a-z0-9-]{3,}/',
        array(
            'controller' => 'category',
            'action'     => 'index'
        )
    );

    // Static post
    $router->add(
        '/[a-z0-9-]{3,}',
        array(
            'controller' => 'post',
            'action'     => 'view',
            'slug'       => 1
        )
    );  

    // Product
    $router->add(
        '/[a-z0-9-]{3,}/([a-z0-9-]{3,})',
        array(
            'controller' => 'product',
            'action'     => 'view',
            'slug'       => 1
        )
    );
    $router->add(
        '/[a-z0-9-]{3,}/[a-z0-9-]{3,}/([a-z0-9-]{3,})',
        array(
            'controller' => 'product',
            'action'     => 'view',
            'slug'       => 1
        )
    );
    $router->add(
        '/[a-z0-9-]{3,}/[a-z0-9-]{3,}/[a-z0-9-]{3,}/([a-z0-9-]{3,})',
        array(
            'controller' => 'product',
            'action'     => 'view',
            'slug'       => 1
        )
    ); 

如果有人可以优化此效果,请发布为另一个答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM