Now, as we know how Controllers/Views/Routes work, we can get back to the original initial code of the permission editor application, and copy-paste it all into the package folder, just changing the namespaces.
For example, our full RoleController will look like this:
packages/laraveldaily/laravel-permission-editor/src/Http/Controllers/RoleController.php:
namespace Laraveldaily\LaravelPermissionEditor\Http\Controllers; use Illuminate\Http\Request;use Illuminate\Routing\Controller;use Spatie\Permission\Models\Role;use Spatie\Permission\Models\Permission; class RoleController extends Controller{ public function index() { $roles = Role::withCount('permissions')->get(); return view('permission-editor::roles.index', compact('roles')); } public function create() { $permissions = Permission::pluck('name', 'id'); return view('permission-editor::roles.create', compact('permissions')); } public function store(Request $request) { $request->validate([ 'name' => ['required', 'string', 'unique:roles'], 'permissions' => ['array'], ]); $role = Role::create(['name' => $request->input('name')]); $role->givePermissionTo($request->input('permissions')); return redirect()->route('permission-editor.roles.index'); } public function edit(Role $role) { $permissions = Permission::pluck('name', 'id'); return view('permission-editor::roles.edit', compact('role', 'permissions')); } public function update(Request $request, Role $role) { $request->validate([ 'name' => ['required', 'string', 'unique:roles,name,' . $role->id], 'permissions' => ['array'], ]); $role->update(['name' => $request->input('name')]); $role->syncPermissions($request->input('permissions')); return redirect()->route('permission-editor.roles.index'); } public function destroy(Role $role) { $role->delete(); return redirect()->route('permission-editor.roles.index'); }}
I've also copy-pasted all the files from app/Http/Controllers
and resources/views
from the initial code at the very beginning of this tutorial. Won't paste them here, you can check the final repository for the details, the link will be at the end of the tutorial.
Now, if we relaunch our Roles page, we will get an expected obvious error:
Class "Spatie\Permission\Models\Permission" not found
Of course, our package doesn't tell the outside Laravel application that we expect that package to be installed!
This is where we get back to our composer.json
of the package and need to fill in the Spatie package as a prerequisite required dependency:
{ "name": "laraveldaily/laravel-permission-editor", // ... "require": { "spatie/laravel-permission": "^5.0" }}
For the versions of the required packages, you need to check the releases of the packages and specify the version you need. At the time of writing this tutorial, the latest version of spatie/laravel-permission
is 5.x
, so I specify exactly that.
Now, we need to run composer update
on the main project: it updates not only the dependencies of the main project but also the dependencies of the dependencies. Which means it will install the required package for us.
In my case, I saw two relevant lines in the Terminal:
Package operations: 1 install, 1 update, 0 removals - Installing spatie/laravel-permission (5.7.0): Extracting archive - Upgrading laraveldaily/laravel-permission-editor (dev-main a066e56 => dev-main 274d508): Source already present
And now if we reload the browser... Another error:
Base table or view not found: 1146 Table 'permissions' doesn't exist
Of course. Our package assumes that the Spatie Permission package is not only installed but properly configured. You can check the full details in its official documentation, and for this tutorial, it's not that relevant, as your packages will not rely on Spatie, but for the sake of continuing this demo, I will follow these instructions:
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"php artisan migrate
If you have some similar custom requirements for the package consumer, you should specify them in the readme.md file of your package, as a part of installation instructions.
And now when we reload the browser again...
Yes, we have our table and a link to add a new role!
Still unstyled, of course, and this is exactly our next step.
Notice: if your View files don't reload to the new versions, as it was in my case locally, you may need to run the command php artisan view:clear
to clear the Blade cache.