当我使用带有Post方法的表单请求时,响应是" index()
"方法响应。但它必须是" store(myRequest $request)
"方法。
如果我从" 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']]);
});
邮差回应:
答案 0 :(得分:0)
你应该让你的功能成为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();
}
}