[英]Phalcon routing correctly pattern
幫助路由設置,前端有以下請求模板: /books/([0-9]+)/book-authors/([0-9]+)/images
命名空間中有一個 controller: Shop\Controllers\Books\BookAuthors\ImagesController
controller 有一個 indexAction 方法。
在 routing.php 中,我指定了以下內容:
$router = new Router(false);
$router->removeExtraSlashes(true);
$router->setDefaultNamespace('Shop\Controllers');
$router->setDefaultController('index');
$router->setDefaultAction('index');
$router->addGet('/books/([0-9]+)/book-authors/([0-9]+)/images', [
'namespace' => 'Shop\Controllers\Books\BookAuthors',
'bookId' => 1,
'authorId' => 2,
'controller' => 'images',
'action' => 'index',
]);
return $router;
結果,我們發現重定向總是轉到默認的 controller。請告訴我如何修復...
我嘗試調試並檢查模板不適合的原因,但是當我在網站上檢查 regex101 時,一切都在那里匹配並且應該可以工作,但由於某種原因它在 phalcon 中不起作用。 每次“未找到”時應用程序返回
試試這個:
$router->addGet('/books/:int/book-authors/:int/images', [
'namespace' => 'Shop\Controllers\Books\BookAuthors',
'controller' => 'images',
'action' => 'index',
'bookId' => 1,
'authorId' => 2,
]);
請注意,我不知道路由器定義中是否可以有多個“:int”,我還沒有嘗試過這段代碼。
如果一行中不能有多個":int",你可能需要重組並將bookId和authorId移到最后並使用:params。 請注意,我還刪除了“圖像”controller 名稱,因為您不需要在該行中使用它。
$router->addGet('/books/book-authors/:params', [
'namespace' => 'Shop\Controllers\Books\BookAuthors',
'controller' => 'images',
'action' => 'index',
'params' => 1,
]);
對於 bookID 98 和 authorId 212,您的 URL 將類似於“/books/book-authors/98/212”。
該路線工作正常,但您可以為簡單明了而嘗試這樣做:
$router->addGet('/books/{bookId:[0-9]+}/book-authors/{authorId:[0-9]+}/images',
[
'controller' => 'images',
'action' => 'index'
]
);
在您的ImagesController
中將indexAction
定義為:
public function indexAction(int $bookId, int $authorId)
{
echo "BookId: $bookId and AuthorId: $authorId";
}
對於/books/10/book-authors/22/images
,結果應該是:
BookId: 10 and AuthorId: 22
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.