Someone please help me enlighten in LARAVEL !!!
in LARAVEL controller i difine static function like this :
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 ...;
}
}
And then i must use controll like :
if ($method == 'get')
$ctrl::read($request);
if ($method == 'post')
$ctrl::post($request);
$ctrl::put($request);
But i have a array :
and i want to be like this :
$acceptMethod = ['GET','POST','PUT','DELETE'];
foreach($acceptMethod as $functionName) {
// Not work
$ctrl::$functionName($request);
}
Is there any way to make this possible ??
Use {};
Please, try this inside the loop:
$fn = strtolower($functionName)
$ctrl::{$fn}($request);
You can call a attribute too..
$instance->{'attribute_name'};
The proper way to do it would be to define a RESTful resource for your object so you get all the routes RESTfully. In your routes/api.php
Route::resource('thing','MyAPIController');
That will magically route:
If you have multiple objects to REST, you'd just add a controller for each.
Injection is always on the OWASP top 10 each year, and this opens up potential function injection. You can mitigate that risk by making sure you white list the method. But, I'd rather just use Laravel the way it was intended.