Last active
March 3, 2023 14:38
-
-
Save diloabininyeri/71c9b9b1e44eb67d895c879a4c9f12d0 to your computer and use it in GitHub Desktop.
I have sampled how Laravel route architecture is in the simplest way, after this example you can make a very advanced route yourself
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 | |
class RouteManager | |
{ | |
private string $middleware; | |
private string $name; | |
public function __construct(private readonly string $basePath, private readonly array $callable) | |
{ | |
} | |
public function middleware(string $middleware): self | |
{ | |
$this->middleware = $middleware; | |
return $this; | |
} | |
public function name(string $name): self | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getMiddleware(): string | |
{ | |
return $this->middleware; | |
} | |
/** | |
* @return string | |
*/ | |
public function getName(): string | |
{ | |
return $this->name; | |
} | |
/** | |
* @return string | |
*/ | |
public function getBasePath(): string | |
{ | |
return $this->basePath; | |
} | |
/** | |
* @return array | |
*/ | |
public function getCallable(): array | |
{ | |
return $this->callable; | |
} | |
} | |
class Route | |
{ | |
/** | |
* @var array<RouteManager> | |
*/ | |
private static array $routes = []; | |
/** | |
* @return array | |
*/ | |
public static function getRoutes(): array | |
{ | |
return self::$routes; | |
} | |
public static function get(string $basePath, array $callable): RouteManager | |
{ | |
return static::$routes[] = new RouteManager($basePath, $callable); | |
} | |
} | |
class XController | |
{ | |
public function foo() | |
{ | |
return 'foo'; | |
} | |
} | |
Route::get('/', [XController::class, 'foo'])->middleware('web')->name('home'); | |
Route::get('/profile', [XController::class, 'foo'])->middleware('web')->name('profile'); | |
foreach (Route::getRoutes() as $route) { | |
$route->getMiddleware(); | |
$route->getName(); | |
$route->getBasePath(); | |
$route->getCallable(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment