Laravel-modules: Policies : authorizeresource does not exist

Created on 17 Mar 2020  路  2Comments  路  Source: nWidart/laravel-modules

Hi,
I'm using Laravel ^6.2 and the latest version of this package.
I put a folder for Policies into Entities of a module.
I pust this method into PostController, same as Laravel doc.

    public function __construct()
    {
        $this->authorizeResource(Post::class, 'post');
    }

But unfortunately I get an error:

authorizeresource does not exist

Please could you set us an example for this. Thanks.

Most helpful comment

The generated controller extends the default Illuminate\Routing\Controller not the usual App\Http\Controllers\Controller as in a fresh Laravel installation.

To be able to use authorization related methods you have to use the Illuminate\Foundation\Auth\Access\AuthorizesRequests trait in your class:

<?php
namespace Module\Posts\Http\Controllers;

// Add this use statement
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class PostController extends Controller
{
    use AuthorizesRequests;

    public function __construct()
    {
        $this->authorizeResource(Post::class, 'post');
    }

    // ...
}

All 2 comments

The generated controller extends the default Illuminate\Routing\Controller not the usual App\Http\Controllers\Controller as in a fresh Laravel installation.

To be able to use authorization related methods you have to use the Illuminate\Foundation\Auth\Access\AuthorizesRequests trait in your class:

<?php
namespace Module\Posts\Http\Controllers;

// Add this use statement
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class PostController extends Controller
{
    use AuthorizesRequests;

    public function __construct()
    {
        $this->authorizeResource(Post::class, 'post');
    }

    // ...
}

Thank you so much

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cristiangervasi picture cristiangervasi  路  12Comments

KyawNaingTun picture KyawNaingTun  路  16Comments

esipavicius picture esipavicius  路  13Comments

designvoid picture designvoid  路  10Comments

zcwilt picture zcwilt  路  11Comments