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? (36 h 00 min)

You also get:

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

Already a member? Login here

Firmansyah avatar

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

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

Firmansyah avatar

Solved. It is composer thing, now its work

👍 1
copelojr avatar

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

👍 1
SquareNetMedia avatar

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

Povilas Korop avatar

Maybe try composer dump-autoload?

👍 1
oussama frp avatar

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'); ?

Povilas Korop avatar

Good point, probably your suggestion would work better.

ignacio-dev avatar

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!!

Povilas Korop avatar

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_r avatar

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

Modestas avatar

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