簡體   English   中英

PHP-Laravel中帶參數的路由

[英]PHP - Routing with Parameters in Laravel

我正在嘗試使用Laravel創建RESTful API。

在我的routes.php

Route::get('/accounts/(:any?)', array('as'=>'account_index', 'uses'=>'accounts@index'));

我的控制器:

class Accounts_Controller extends Base_Controller {
public $restful = true;

public function get_index($id = null) {
    if(!$id)
        return Response::json(Account::all());
    return Response::json(Account::find($id));
}

嘗試任何請求accounts/## ,我都會收到404響應,但是accounts工作正常。 當我將路線更改為非以下accounts

Route::get('/accts/(:any?)'

我的路由可以按預期工作,並且最重要的是,發送到accounts請求仍然可以正常工作。 是因為我將get_index用作函數名,以便它恢復為使用標准的http://localhost/controller/method/arguments嗎?

編輯我有控制器被自動檢測到:

Route::controller(Controller::detect());

定義路由時,定義這些路由的順序很重要。 Laravel使用正則表達式將請求的URI與這些模式進行匹配,並且使用第一個要匹配的URI而不進行進一步處理。

Route::controller('accounts')有效地匹配accounts/(:any?)/(:any?)/(:any?)等。如果要測試URL accounts/index/12您將得到預期的結果結果。

Route::get('/accounts/(:any?)', array('as'=>'account_index', 'uses'=>'accounts@index'));
Route::controller( Controller::detect() );

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM