Skip to content

Commit

Permalink
Merge pull request #63 from msmakouz/feature/routes-bootloader
Browse files Browse the repository at this point in the history
Adding RoutesBootloader, removing LocaleSelectorBootloader
  • Loading branch information
butschster authored Jun 22, 2022
2 parents 9c1e8e9 + c3ad37a commit b818c58
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 63 deletions.
20 changes: 20 additions & 0 deletions app/routes/web.php
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);
};
1 change: 0 additions & 1 deletion app/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class App extends Kernel
* Application specific services and extensions.
*/
protected const APP = [
Bootloader\LocaleSelectorBootloader::class,
Bootloader\RoutesBootloader::class,

// fast code prototyping
Expand Down
24 changes: 0 additions & 24 deletions app/src/Bootloader/LocaleSelectorBootloader.php

This file was deleted.

90 changes: 52 additions & 38 deletions app/src/Bootloader/RoutesBootloader.php
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,
]);
}
}

0 comments on commit b818c58

Please sign in to comment.