Skip to main content

Our First Controller and Route

Premium
3 min read

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.

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 46 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

F
Firmansyah ✓ Link copied!

Hi, I do exactly as a tutorial, but I got an error.

Target class [Laraveldaily\LaravelPermissionEditor\Http\Controllers\RoleController] does not exist.

F
Firmansyah ✓ Link copied!

Solved. It is composer thing, now its work

C
copelojr ✓ Link copied!

Hello. Could you explaing how did you solve the composer thing?

S
SquareNetMedia ✓ Link copied!

I'm getting the same error - how did you resolve this?

PK
Povilas Korop ✓ Link copied!

Maybe try composer dump-autoload?

OF
oussama frp ✓ Link copied!

since the service provider in the same directory as routes folder, why $this->loadRoutesFrom(__DIR__ . '/../routes/web.php'); instead of $this->loadRoutesFrom(__DIR__ . '/routes/web.php'); ?

PK
Povilas Korop ✓ Link copied!

Good point, probably your suggestion would work better.

I
ignacio-dev ✓ Link copied!

Thank you for this course! It's unclear to me where exactly we would write unit tests for our package, and where would we trigger those tests from.

Would you mind clarifying that? Thank you!!

PK
Povilas Korop ✓ Link copied!

Sorry I don't have a QUICK answer for you and I don't have specific tutorial for this, please google "create laravel package unit tests", there are quite a lot written about it elsewhere online.

F
F_r ✓ Link copied!

how are the routes from the package loaded inside the main routing of Laravel? Is there a merging happening somewhere under the hood?

M
Modestas ✓ Link copied!

In the PermissionEditorServiceProvider we are registering routes using Route:: class. This adds the routes to the combined list (merging them)

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.