Courses

How to Create Laravel Package: Step-by-Step Example

Let's finally test if our package does something (anything, really), by creating a Controller.

Let's create this file manually, cause it's not worth using make:controller - you would spend more time changing the namespaces and removing the default BaseController functionality if you don't need it.

So, we create a separate src/Http/Controllers subfolder inside our package, and create this:

packages/laraveldaily/laravel-permission-editor/src/Http/Controllers/RoleController.php:

namespace Laraveldaily\LaravelPermissionEditor\Http\Controllers;
 
use Illuminate\Routing\Controller;
 
class RoleController extends Controller
{
 
public function index()
{
return 'It works!';
}
 
}

Important note: we extend from the Illuminate\Routing\Controller from Laravel core, but the default Laravel project extends their Controllers from the BaseController in the app/Http/Controllers folder. In some cases, some packages may need the same functionality as that Controller's traits, but in our case, we don't need it, so the core Laravel Routing Controller is fine.

Now, how do we create a Route for what Controller? Simply by creating a familiar routes/web.php file, just inside the package structure.

This lesson is only for Premium Members.
Want to access all lessons of this course?

You also get:

  • 59 courses (1056 lessons, 44 h 09 min total)
  • Premium tutorials
  • Access to repositories
  • Private Discord