-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from msmakouz/feature/routes-bootloader
Adding RoutesBootloader, removing LocaleSelectorBootloader
- Loading branch information
Showing
4 changed files
with
72 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Controller\HomeController; | ||
use Spiral\Router\Loader\Configurator\RoutingConfigurator; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Web Routes | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Here is where you can register web routes for your application. These | ||
| routes are loaded by the Router within a group which | ||
| contains the "web" middleware group. | ||
| | ||
*/ | ||
return function (RoutingConfigurator $routes) { | ||
$routes->add('html', '/<action>.html')->controller(HomeController::class); | ||
}; |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,66 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of Spiral package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Bootloader; | ||
|
||
use App\Controller\HomeController; | ||
use Spiral\Boot\Bootloader\Bootloader; | ||
use Spiral\Router\Route; | ||
use Spiral\Router\RouteInterface; | ||
use Spiral\Router\RouterInterface; | ||
use Spiral\Router\Target\Controller; | ||
use Spiral\Router\Target\Namespaced; | ||
use App\Middleware\LocaleSelector; | ||
use Spiral\Auth\Middleware\AuthTransportMiddleware; | ||
use Spiral\Boot\DirectoriesInterface; | ||
use Spiral\Bootloader\Http\RoutesBootloader as BaseRoutesBootloader; | ||
use Spiral\Cookies\Middleware\CookiesMiddleware; | ||
use Spiral\Core\Container\Autowire; | ||
use Spiral\Csrf\Middleware\CsrfMiddleware; | ||
use Spiral\Debug\StateCollector\HttpCollector; | ||
use Spiral\Http\Middleware\ErrorHandlerMiddleware; | ||
use Spiral\Http\Middleware\JsonPayloadMiddleware; | ||
use Spiral\Router\Loader\Configurator\RoutingConfigurator; | ||
use Spiral\Session\Middleware\SessionMiddleware; | ||
|
||
class RoutesBootloader extends Bootloader | ||
final class RoutesBootloader extends BaseRoutesBootloader | ||
{ | ||
public function boot(RouterInterface $router): void | ||
public function __construct( | ||
private readonly DirectoriesInterface $dirs | ||
) { | ||
} | ||
|
||
protected function globalMiddleware(): array | ||
{ | ||
// named route | ||
$router->setRoute( | ||
name: 'html', | ||
route: new Route('/<action>.html', new Controller(HomeController::class)) | ||
); | ||
|
||
// fallback (default) route | ||
$router->setDefault($this->defaultRoute()); | ||
return [ | ||
ErrorHandlerMiddleware::class, | ||
JsonPayloadMiddleware::class, | ||
HttpCollector::class, | ||
LocaleSelector::class, | ||
]; | ||
} | ||
|
||
/** | ||
* Default route points to namespace of controllers. | ||
*/ | ||
protected function defaultRoute(): RouteInterface | ||
protected function middlewareGroups(): array | ||
{ | ||
// handle all /controller/action like urls | ||
$route = new Route( | ||
pattern: '/[<controller>[/<action>]]', | ||
target: new Namespaced('App\\Controller') | ||
); | ||
|
||
return $route->withDefaults([ | ||
'controller' => 'home', | ||
'action' => 'index', | ||
]); | ||
return [ | ||
'web' => [ | ||
CookiesMiddleware::class, | ||
SessionMiddleware::class, | ||
CsrfMiddleware::class, | ||
// new Autowire(AuthTransportMiddleware::class, ['transportName' => 'cookie']) | ||
], | ||
'api' => [ | ||
// new Autowire(AuthTransportMiddleware::class, ['transportName' => 'header']) | ||
], | ||
]; | ||
} | ||
|
||
protected function defineRoutes(RoutingConfigurator $routes): void | ||
{ | ||
$routes->import($this->dirs->get('app') . '/routes/web.php')->group('web'); | ||
|
||
$routes->default('/[<controller>[/<action>]]') | ||
->namespaced('App\\Controller') | ||
->defaults([ | ||
'controller' => 'home', | ||
'action' => 'index', | ||
]) | ||
->middleware([ | ||
// SomeMiddleware::class, | ||
]); | ||
} | ||
} |