Запрос формы Laravel: плохой метод вызывается

Когда я использую запрос формы с методом Post, ответом будет «index()«Метод ответа. Но это должно быть»store(myRequest $request)метод.
Если я удалю метод myRequest $ request из «store()«Это работает. Я потерян. Пожалуйста, помогите мне.

Мой контроллер:

<?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'
];
}
}

Извлечь из rout.php:

Route::group(array('namespace' => 'Ressource', 'prefix' => 'ressource'), function () {
Route::resource('collection', 'CollectionController', ['only' => ['index', 'show', 'store', 'update', 'destroy']]);
});

Почтальон запрос:
Почтальон запрос

Почтальон ответ:

Ответ почтальона

4

Решение

Вы должны сделать свою функцию функцией 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();
}

}

0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]