Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route attributes register example #589

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion blog/config/common/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

declare(strict_types=1);

return [];
return [
static function (Psr\Container\ContainerInterface $container) {
$routeRegistrar = $container->get(\Yiisoft\Router\RouteAttributesRegistrarInterface::class);
$routeRegistrar->register();
},
];
5 changes: 3 additions & 2 deletions blog/config/common/di/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
$collector
->middleware(CsrfMiddleware::class)
->middleware(FormatDataResponse::class)
->addGroup(
->addRoute(
Group::create('/{_language}')
->routes(...$config->get('routes'))
->routes(...$config->get('routes'))
);

if (!str_starts_with(getenv('YII_ENV') ?: '', 'prod')) {
Expand All @@ -29,4 +29,5 @@

return new RouteCollection($collector);
},
\Yiisoft\Router\RouteAttributesRegistrarInterface::class => \Yiisoft\Router\RouteAttributesRegistrar::class,
];
4 changes: 2 additions & 2 deletions blog/config/common/routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@

return [
// Lonely pages of site
Route::get('/')
/*Route::get('/')
->action([SiteController::class, 'index'])
->name('site/index'),
->name('site/index'),*/
Route::methods([Method::GET, Method::POST], '/contact')
->action([ContactController::class, 'contact'])
->name('site/contact'),
Expand Down
4 changes: 4 additions & 0 deletions blog/src/Controller/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
namespace App\Controller;

use Psr\Http\Message\ResponseInterface;
use Yiisoft\Router\Attribute\Get;
use Yiisoft\Router\Group;
use Yiisoft\Yii\View\ViewRenderer;

#[Group('/{_language}')]
final class SiteController
{
public function __construct(private ViewRenderer $viewRenderer)
{
$this->viewRenderer = $viewRenderer->withController($this);
}

#[Get('/', 'site/index')]
public function index(): ResponseInterface
{
return $this->viewRenderer->render('index');
Expand Down