Last active
March 27, 2018 03:59
-
-
Save afiqiqmal/442e3aa7a5f34d0d836131249426628f to your computer and use it in GitHub Desktop.
Custom Router using Macro for laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Macros; | |
interface MacroContract | |
{ | |
public static function registerMacros(); | |
public function createMacros(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Macros\Router; | |
use App\Macros\MacroContract; | |
use Illuminate\Support\Facades\Artisan; | |
use Illuminate\Support\Facades\Route; | |
use Illuminate\Support\Str; | |
class RouterPrint implements MacroContract | |
{ | |
public static function registerMacros() | |
{ | |
(new RouterPrint)->makeNewRoute(); | |
} | |
public function makeNewRoute() | |
{ | |
// Route::source('name', 'middleware array', 'namespace', 'namespace for folder to be created') | |
if (!Route::hasMacro('source')) { | |
Route::macro('source', function ($module, $namespace = null, $currentNameSpace = null, array $middleware = ['auth']) { | |
$module = strtolower($module); | |
$url = str_replace('.', '/', Str::singular($module)); | |
$name = Str::singular($module); | |
$controller = Str::studly(str_replace('.', ' ', Str::singular($module))) . 'Controller'; | |
$currentNameSpace = $currentNameSpace ? ucwords($currentNameSpace) : null; | |
$namespace_with_controller = ($currentNameSpace ? $currentNameSpace."\\" : null).$controller; | |
//generate controller by running route:list, only create once if already existed | |
Artisan::call('resource:controller', [ | |
'name' => $namespace_with_controller, | |
]); | |
//distinguish when the module have [dot] | |
$prefix = null; | |
if (strpos($url, '/')) { | |
$build_prefix = explode('/', $url); | |
if (count($build_prefix) > 1) { | |
$prefix_array = array_slice($build_prefix, 0, -1); | |
$prefix = implode("/", $prefix_array); | |
$url = end($build_prefix); | |
} | |
} | |
Route::group([ | |
'prefix' => $prefix, //route prefix | |
'namespace' => $namespace, //path | |
'middleware' => $middleware, //middleware | |
], function () use ($url, $name, $controller, $module) { | |
Route::get($url . '/', $controller . '@index') | |
->name($module); | |
Route::get($url . '/{id}/edit', $controller . '@edit') | |
->name($module . '.edit'); | |
Route::get($url . '/{id}/show', $controller . '@show') | |
->name($module . '.show'); | |
Route::get($url . '/create', $controller . '@create') | |
->name($module . '.create'); | |
Route::patch($url . '/{id}/update', $controller . '@update') | |
->name($module . '.update'); | |
Route::post($url . '/store', $controller . '@store') | |
->name($module . '.store'); | |
Route::delete($url . '/{id}/delete', $controller . '@destroy') | |
->name($module . '.destroy'); | |
}); | |
}); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// create ApplicationController and it routes | |
Route::source('application'); | |
// create CMS/ApplicationController located in CMS folder | |
Route::source('application', null, 'CMS'); | |
// create DIRECTORY/ApplicationController and auto link with current namespace ex: web, api, etc.... | |
Route::source('application', 'API', 'DIRECTORY'); | |
// create ApplicationUserController with route name ex: application.user, application.user.store, application.user.update, etc... | |
Route::source('application.user'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment