Laravel 表单请求:调用错误的方法

Posted

技术标签:

【中文标题】Laravel 表单请求:调用错误的方法【英文标题】:Laravel Form Request : bad method be called 【发布时间】:2016-04-14 04:17:19 【问题描述】:

当我使用带有 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());
        );
    

资源控制器:

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

邮递员请求:

邮递员回复:

【问题讨论】:

【参考方案1】:

你应该让你的函数变成 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));

资源控制器:

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

【讨论】:

我希望这是解决方案,但“Str::contains($callback, 'SerializableClosure')”返回错误:“strpos() expects parameter 1 to be string, object given 我想我错过了图书馆。请再检查一次。我以前也遇到过像你这样的问题,这种方法对我有帮助。 在 buildCallable 之后尝试 var_dump 变量 $callback 函数。 我试了一下,它什么也没做。函数buildCallable 永远不会被调用。我认为这是因为 store(CreateCollectionRequest $request) 也从未被调用过。 如果函数 buildCallable 从不调用为什么会出现错误 strpos() expects parameter 1 to be string, object given

以上是关于Laravel 表单请求:调用错误的方法的主要内容,如果未能解决你的问题,请参考以下文章

尝试通过 AJAX 验证 Laravel 表单时出现错误 422

Laravel 记录表单请求验证实际错误消息的最简单方法

方法验证不存在 - Laravel 5.4

使用Larave5.6l提交POST请求出现The page has expired due to inactivity错误

使用 laravel 请求类时,在数组错误上调用成员函数失败()

Larave中CSRF攻击