Phalcon PHP - 如何自定义路由以使网址友好,如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

在法尔孔。

谢谢。

您可以在依赖于自定义函数的路线上使用转化

// 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);
    }
  );

感谢尼古拉斯·迪莫普洛斯,您的响应意味着您将 slugs 转换为有效函数。我找到了我问题的答案(我的项目中有 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
        )
    ); 

如果有人可以优化这一点,请发布作为另一个答案。