php - Laravel 表单请求 : bad method be called

标签 php laravel laravel-5 laravel-5.1 postman

当我使用带有 Post 方法的表单请求时,响应是“index()”方法响应。但它必须是“store(myRequest $request)”方法。 如果我从“store()”中删除 myRequest $request 方法,它就可以工作。我迷路了。请帮助我。

我的 Controller :

<?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());
        });
    }
}

资源 Controller :

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

postman 要求: Postman request

postman 回复:

Postman reponse

最佳答案

您应该使您的函数成为 Clouse 函数。 我的 Controller :

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));
}

资源 Controller :

<?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 表单请求 : bad method be called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694712/

相关文章:

laravel - 如何通过公共(public) URL 从存储中获取文件?

laravel - 如何将字符串连接到 Blade 中的$变量?

PHP:对象的array_filter?

php - 干预图片圆角上传

PHP session 不会过期。曾经

php - 似乎无法在 PHP 中获得 mkdir() 的正确权限

Laravel Vue SPA 使用 Sanctum 响应未经授权

php - 试图让电子邮件在 Laravel 5 中工作

php - 使用 PHP 和 MySQL 的 Android 应用程序 - Dreamweaver 显示 root 错误

php - 带时间戳的排序记录