簡體   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