2015-03-03 27 views
0

假設我有一個控制器,它看起來像這樣:的自定義網址相同的控制器

AController.php

<?php namespace App\Http\Controllers; 

use App\Http\Controllers\Controller; 

class AController extends Controller { 

    public function doThis(){...} 
    public function doThat(){...} 
    public function doThing(){...} 
} 

routes.php文件

Route::get('/doThis', [ 
    'as' => 'acontroller.dothis', 'uses' => '[email protected]' 
]); 

Route::get('/doThis', [ 
    'as' => 'acontroller.dothat', 'uses' => '[email protected]' 
]); 

Route::get('/doThis', [ 
    'as' => 'acontroller.dothing', 'uses' => '[email protected]' 
]); 

有沒有更好的辦法比使用Route::get()?我想我的路線是自動ControllerName.methodName和URL是/methodName而無需顯式使用Route::get()

回答

1

你正在尋找一個「隱控制器」(文檔here)。

如果定義您的路線,如:

Route::controller('/', 'AController'); 

所有指定的前綴(第一個參數)下方的路線將被路由到該控制器。然後,Laravel期望方法名稱被定義爲HTTP動詞和路由的組合。

所以,您的控制器將是:

class AController extends Controller { 
    public function getDoThis(){...} // GET to /doThis 
    public function postDoThat(){...} // POST to /doThat 
    public function anyDoThing(){...} // any verb to /doThing 
} 
+0

我看着太,但如何指定的路線?例如['as'=>'ControllerName.methodName'] – 2015-03-03 02:31:24

+0

@Margo您不必。路線由控制器上定義的方法隱含。通過定義'getDoThis'方法,隱式定義'/ doThis'路由。 – patricus 2015-03-03 03:01:08

+0

其實我剛學會使用隱式控制器是不好的,因爲你會得到像這樣的'doThis/{one?}/{two?}/{three?}/{four?}/{five?}'路線。我仍然在考慮是否還有另一種方法來避免使用控制器 – 2015-03-03 03:10:11