有人請幫助我啟發拉拉夫!在LARAVEL控制器中,我定義了靜態函數,如下所示:namespace App\Http\Controllers\MyAPI;use Illuminate\Http\Request;use App\Http\Controllers\Controller;class MyAPIController extends Controller { const acceptMethod = ['GET','POST','PUT','DELETE'] public function handler(Request $request) { $acceptMethod = self::acceptMethod; $ctrl = new PromotionController; $method = $request->method() // This is my question :(( if ($method == 'GET') $ctrl::read($request); if ($method == 'GET') $ctrl::post($request); $ctrl::put($request); ... //I want to be like this : foreach($acceptMethod as $method) { // Not work $ctrl::($method)($request); } } public static function read(Request $request) { return something; } public static function post(Request $request) { return ...; } public static function put(Request $request) { return ...; } public static function delete(Request $request) { return ...; }}然后我必須使用controll像: if ($method == 'get') $ctrl::read($request); if ($method == 'post') $ctrl::post($request); $ctrl::put($request);但是我有一個數組:我想成為這樣: $acceptMethod = ['GET','POST','PUT','DELETE']; foreach($acceptMethod as $functionName) { // Not work $ctrl::$functionName($request); }有什么辦法可以做到這一點?
2 回答

德瑪西亞99
TA貢獻1770條經驗 獲得超3個贊
用 {};
請在循環中嘗試以下操作:
$fn = strtolower($functionName)
$ctrl::{$fn}($request);
您也可以調用屬性。
$ instance-> {'attribute_name'};

瀟瀟雨雨
TA貢獻1833條經驗 獲得超4個贊
路線
正確的方法是為您的對象定義一個RESTful資源,以便您獲得所有RESTful路由。在你的路線/ api.php中
Route::resource('thing','MyAPIController');
這將神奇地路由:
獲取api / thing到index()
GET api / thing / create到create()
POST api /要存儲的東西
獲取要顯示的api / thing / {id}($ id)
GET api / thing / {id} / edit to edit()
修補程序api / thing / {id}以進行update()
刪除api / thing / {id} to destroy()
如果您有多個要使用REST的對象,則只需為每個對象添加一個控制器。
怎么了$ ctrl :: {$ fn}($ request)
每年OWASP的注入量始終排在前十位,這開啟了潛在的功能注入。您可以通過確保將方法列入白名單來減輕這種風險。但是,我寧愿按照預期的方式使用Laravel。
- 2 回答
- 0 關注
- 196 瀏覽
添加回答
舉報
0/150
提交
取消