PHP code example of lassehaslev / laravel-package-router

1. Go to this page and download the library: Download lassehaslev/laravel-package-router library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lassehaslev / laravel-package-router example snippets


// It is recommended that you extend the class
class MyRouter extends LasseHaslev\LaravelPackageRouter\PackageRouter {}

// Create new router
$router = MyRouter::create();

// Add route to router
$router->add( 'users.index', [
    'uri'=>'users',
    'as'=>'users.index',
    'uses'=>'Controller@index',
] )
// You can also chain add
->app( 'users.update', [
    'uri'=>'users/{user}',
    'method'=>'put',
    'uses'=>'Controller@index',
] );

->add( 'images.index', [
    'uri'=>'images',
    'uses'=>'Controller@index',
] )
->add( 'images.store', [
    'uri'=>'images',
    'method'=>'post',
    'uses'=>'Controller@store',
] )

// Usually in your routes/web.php
$myRouter = MyRouter::get();

$myRouter->route( 'images.index' ); // "/images"
Route::group([ 'prefix'=>'backend', 'middleware'=>'auth' ], function( $router ) use ( $myRouter ) {
    $myRouter->routes( 'users' ); // "/backend/users" and "/backend/users/{user}"
    $myRouter->route( 'images.store' ); // "/backend/images"
});

// Get the router
$router = MyRouter::create(); // MyRouter::get();

// Add route
$router->add( $reference, [
    'uri'=>'users',
    'method'=>'get',
    'as'=>'users.index',
    'uses'=>'Controller@index',
    // 'middleware'=>'auth', // You can add middleware if you want to
] );

// Get all routes
$router->routes(); // Set routes for users.index, images.index and images.show

// Get all routes under images namespace
$router->routes( 'images' ); // Set routes for images.index and images.show

// Get single route
$router->route( 'images.index' );