php - Laravel function callback -
i need find cues in string , call functions these cues
example
<?php namespace app\http\controllers; class modulescontroller { private $modules = [ ['name' => 'somefunction','maxnumber' => 50] ]; public function checkfunctions($strings = null) { $modulearrayid = array_search($strings ,array_column($this->modules, 'name'); if($modulearrayid !== false) $this->$modules[$modulearrayid]['name']; } public function somefunction() { return "it works"; } }
and resource example
<?php namespace app\http\controllers; use app\http\controllers\modulescontroller; class blockcontroller extends controller { public function __construct() { $this->middleware('auth:members'); } public function index(request $request) { $string = "somefunction"; $callback = new modulescontroller; var_dump($callback->checkfunctions($string)); //always null } }
i have use code this. if can understand this, try improve it.
try this:
public function checkfunctions($strings = null) { $modulearrayid = array_search($strings ,array_column($this->modules, 'name')); if($modulearrayid !== false) { $method = $this->modules[$modulearrayid]['name']; return $this->$method(); } }
not sure how changed code in order post here, suggest to:
- use better names.
- do not instantiate/call directly controller other controller. controllers should used handle requests, else should moved separate class.
- modify structure of
$modules
associative array, can check if method defined index, instead of using array_search. can make code run faster size of structure grows.
example:
private $modules = [ 'somefunction' => [ 'maxnumber' => 50 ] ]; // , check if method defined: if (isset($this->modules[$strings])) { // ... }
Comments
Post a Comment