如果我从“store()”中删除myRequest $request方法,它就可以了.我迷路了.请帮帮我.
我的控制器:
<?PHP namespace App\http\Controllers\Ressource;use App\http\Requests\CreateCollectionRequest;use App\RepositorIEs\CollectionRepository;class CollectionController extends RessourceController { private $collectionRepository; public function __construct(CollectionRepository $collectionRepository) { parent::__construct(); $this->collectionRepository = $collectionRepository; } public function index() { return $this->run( function() { return $this->collectionRepository->all()->get(); }); } public function store(CreateCollectionRequest $request) { return $this->run( function() use ($request) { return $this->collectionRepository->create($request->all()); }); }}
RessourceController:
<?PHP namespace App\http\Controllers\Ressource;use Illuminate\Support\Facades\Response;use App\http\Controllers\Controller;abstract class RessourceController extends Controller{ protected $result = null; public function __construct() { $this->result = new \stdClass(); $this->result->error = 0; $this->result->message = ''; $this->result->service = $this->getService(); $this->result->data = null; } abstract public function getService(); protected function render() { return Response::Json($this->result); } public function missingMethod($parameters = []) { $this->result->err = 404; $this->result->message = 'Service ' . $this->getService() . ' : ' . $parameters . ' non disponible'; return $this->render(); } protected function run($function) { try { $this->result->data = call_user_func($function); } catch (\Exception $e) { $this->result->err = ($e->getCode() > 0) ? $e->getCode() : -1; $this->result->message = $e->getMessage(); } return $this->render(); }}
自定义表格请求:
namespace App\http\Requests;use App\http\Requests\Request;class CreateCollectionRequest extends Request{ public function authorize() { return true; } public function rules() { return [ 'label' => 'required|Alpha_num|min:3|max:32','description' => 'Alpha_dash|max:65000','parent_collection_ID' => 'exists:collections,ID' ]; }}
从routes.PHP中提取:
Route::group(array('namespace' => 'Ressource','prefix' => 'ressource'),function () { Route::resource('collection','CollectionController',['only' => ['index','show','store','update','destroy']]);});
邮差要求:
邮差响应:
解决方法 你应该让你的功能成为Clouse功能.我的控制器:
use App\http\Requests\CreateCollectionRequest;use App\RepositorIEs\CollectionRepository;use SuperClosure\Serializer;use Illuminate\Support\Str;use Closure;class CollectionController extends RessourceController {private $collectionRepository;public function __construct(CollectionRepository $collectionRepository){ parent::__construct(); $this->collectionRepository = $collectionRepository;}public function index(){ return $this->run( function() { return $this->collectionRepository->all()->get(); });}protected function buildCallable($callback) { if (! $callback instanceof Closure) { return $callback; } return (new Serializer)->serialize($callback);}public function store(CreateCollectionRequest $request){ $callback = function() use ($request) { return $this->collectionRepository->create($request->all()); } return $this->run($this->buildCallable($callback));}
}
RessourceController:
<?PHP namespace App\http\Controllers\Ressource; use Illuminate\Support\Facades\Response; use App\http\Controllers\Controller; use SuperClosure\Serializer; use Illuminate\Support\Str; use Closure;abstract class RessourceController extends Controller{protected $result = null;public function __construct(){ $this->result = new \stdClass(); $this->result->error = 0; $this->result->message = ''; $this->result->service = $this->getService(); $this->result->data = null;}abstract public function getService();protected function render(){ return Response::Json($this->result);}public function missingMethod($parameters = []){ $this->result->err = 404; $this->result->message = 'Service ' . $this->getService() . ' : ' . $parameters . ' non disponible'; return $this->render();}protected function getCallable($callback){ if (Str::contains($callback,'SerializableClosure')) { return unserialize($callback)->getClosure(); } return $callback;}protected function run($function){ try { $this->result->data = call_user_func($this->getCallable($function)); } catch (\Exception $e) { $this->result->err = ($e->getCode() > 0) ? $e->getCode() : -1; $this->result->message = $e->getMessage(); } return $this->render();}
}
总结以上是内存溢出为你收集整理的php – Laravel表单请求:调用坏方法全部内容,希望文章能够帮你解决php – Laravel表单请求:调用坏方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)