mardi 27 février 2018

How to add custom action in laravel backpack with crud request?

Created controller actions

public function publish(PublishArticleRequest $request)
{
    $id = $request->get('id');
    /**
     * @var Article $article
     */
    $article = $this->crud->getEntry($id);
    $article->publish();
    die;
    return back();
}

public function unpublish(PublishArticleRequest $request)
{
    $id = $request->get('id');
    /**
     * @var Article $article
     */
    $article = $this->crud->getEntry($id);
    $article->unpublish();
    $article->save();
    return back();
}

and button

return sprintf(
            '<a href="%s" class="btn btn-xs btn-default">'
            . '<i class="fa fa-fw fa-' . $icon . '"'
            . ' title="' . $title . '"'
            . ' data-toggle="tooltip"'
            . ' data-placement="top"'
            . '></i>'
            . '</a>',
            e(route('crud.article.' . $action, ['id' => $article->getKey()]))
        );

and request

class PublishArticleRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id' => 'required|integer',
        ];
    }
}

and finally route

Route::get('article/{id}/publish', ArticleCrudController::class . '@publish')
    ->where(
        [
            'id' => '[0-9]+'
        ])
    ->name('crud.article.publish');
Route::get('article/{id}/unpublish', ArticleCrudController::class . '@unpublish')
    ->where(
        [
            'id' => '[0-9]+'
        ])
    ->name('crud.article.unpublish');

But publish method in controller is not called. What i forgot? I need add acl-check and csrf-check, but this does not work even without the above checks.

Spam a little - stackoverflow wants more text in question, I didnt know what to say except what was already said, so I wrote this text, just for stackoverflow checking. Sorry for that.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2BSzlzO
via IFTTT

Aucun commentaire:

Enregistrer un commentaire