mojtabaahn / laravel-controller-routes

Adds Controller Centered Routes to Laravel

Stars
0

Laravel Controller Aware Routes

Latest Version on Packagist
Total Downloads
Repo Size
Repo Size

Requirement

This package requires PHP 7.4 or higher.

Installation

You can install the package via composer:

composer require mojtabaahn/laravel-controller-routes

Usage

<?php
// routes/web.php

use MojtabaaHN\LaravelControllerRoutes\Routes;
use Illuminate\Support\Facades\Route;


Routes::make('UserController')->methods(function (ControllerAwareRouter $router) {
    $router->get('user/{user}', 'profile')->name('user.profile');
    $router->get('user/{user}/post/{post}','post')->name('user.post');
});

// Or

Routes::make()
    ->controller('UserController')
    ->methods(function (ControllerAwareRouter $router) {
        $router->get('user/{user}', 'profile')->name('user.profile');
        $router->get('user/{user}/post/{post}','post')->name('user.post');
    });

// Same as

Route::get('user/{user}', '[email protected]')->name('user.profile');
Route::get('user/{user}/post/{post}','[email protected]')->name('user.posts');

// Using RouteRegistrar methods

Routes::make()
    ->prefix('user/{user}')
    ->name('user.')
    ->middleware('web')
    ->controller('UserController')
    ->methods(function (ControllerAwareRouter $router) {
        $router->get('/', 'profile')->name('profile');
        $router->get('posts','posts')->name('posts');
    });

// same as 

Route::prefix('user/{user}')
    ->name('user.')
    ->middleware('web')
    ->group(function(){
        Route::get('/', '[email protected]')->name('profile');
        Route::get('posts','[email protected]')->name('posts');
    });

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.