pla*_*kos 10 php symfony easyadmin symfony5
我在本地安装了新的 Symfony 5 项目,并添加了 Easy Admin trought Symfony CLI:
symfony composer req admin
我应该有/admin路线但它不见了
我跑:
symfony console cache:clear
symfony composer dump-autoload
rm -rf var/cache/*
symfony console debug:router
-------------------------- -------- -------- ------ -----------------------------------
Name Method Scheme Host Path
-------------------------- -------- -------- ------ -----------------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
_wdt ANY ANY ANY /_wdt/{token}
_profiler_home ANY ANY ANY /_profiler/
_profiler_search ANY ANY ANY /_profiler/search
_profiler_search_bar ANY ANY ANY /_profiler/search_bar
_profiler_phpinfo ANY ANY ANY /_profiler/phpinfo
_profiler_search_results ANY ANY ANY /_profiler/{token}/search/results
_profiler_open_file ANY ANY ANY /_profiler/open
_profiler ANY ANY ANY /_profiler/{token}
_profiler_router ANY ANY ANY /_profiler/{token}/router
_profiler_exception ANY ANY ANY /_profiler/{token}/exception
_profiler_exception_css ANY ANY ANY /_profiler/{token}/exception.css
homepage ANY ANY ANY /
-------------------------- -------- -------- ------ -----------------------------------
Run Code Online (Sandbox Code Playgroud)
// config/routes/easy_admin.yaml
easy_admin_bundle:
resource: '@EasyAdminBundle/Controller/EasyAdminController.php'
prefix: /admin
type: annotation
Run Code Online (Sandbox Code Playgroud)
symfony console router:match /admin
[ERROR] None of the routes match the path "/admin"
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
Jul*_* B. 11
您至少需要创建一个仪表板。尝试:
php bin/console make:admin:dashboard
Run Code Online (Sandbox Code Playgroud)
接下来,您可以使用以下命令创建一个 CrudController:
php bin/console make:admin:crud
Run Code Online (Sandbox Code Playgroud)
https://symfony.com/doc/master/bundles/EasyAdminBundle/dashboards.html
是的,我现在正在阅读这本书并遇到了同样的问题。
首先,确保您的工作目录是干净的(运行“ git status ”并删除 EasyAdminBundle 设置所做的所有更改)。
然后运行:
composer require easycorp/easyadmin-bundle:2.*
Run Code Online (Sandbox Code Playgroud)
安装 EasyAdminBundle 版本 2;使用此版本,您可以按照书中所述进行操作。
正如其他人已经说过的,您需要创建一个仪表板:
php bin/console make:admin:dashboard
然后创建至少一个 CRUD 控制器。由于您似乎正在使用 Fast Track 书籍,因此您需要键入以下命令两次,并为评论和会议创建一个命令:
php bin/console make:admin:crud
我也在使用快速通道书,这就是我的文件的结局。我仍在学习 Easy admin3,因此不主张最佳实践,但这应该使菜单看起来像快速通道屏幕截图中的那样:
src/Controller/Admin/DashboardController.php:
/**
* @Route("/admin", name="admin")
*/
public function index(): Response
{
$routeBuilder = $this->get(CrudUrlGenerator::class)->build();
return $this->redirect($routeBuilder->setController(ConferenceCrudController::class)->generateUrl());
}
public function configureDashboard(): Dashboard
{
return Dashboard::new()
->setTitle('Guestbook');
}
public function configureMenuItems(): iterable
{
yield MenuItem::linktoRoute('Back to website', 'fa fa-home', 'homepage');
yield MenuItem::linkToCrud('Conference', 'fa fa-map-marker', Conference::class);
yield MenuItem::linkToCrud('Comment', 'fa fa-comment', Comment::class);
}
Run Code Online (Sandbox Code Playgroud)
src/Controller/Admin/CommentCrudController.php:
public static function getEntityFqcn(): string
{
return Comment::class;
}
public function configureFields(string $pageName): iterable
{
return [
IdField::new('id')->hideOnForm(),
TextField::new('author'),
TextareaField::new('text')->hideOnIndex(), // Removing ->hideOnIndex() will display a link to a text modal
EmailField::new('email'),
DateTimeField::new('createdAt')->hideOnForm(),
ImageField::new('photoFilename', 'Photo')->setBasePath('/uploads/photos')->hideOnForm(),
AssociationField::new('conference')
];
}
Run Code Online (Sandbox Code Playgroud)
src/Controller/Admin/ConferenceCrudController.php
public static function getEntityFqcn(): string
{
return Conference::class;
}
public function configureFields(string $pageName): iterable
{
return [
IdField::new('id')->hideOnForm(),
TextField::new('city'),
TextField::new('year'),
BooleanField::new('isInternational'),
IntegerField::new('commentCount', 'Comments')->hideOnForm()
];
}
Run Code Online (Sandbox Code Playgroud)
在 src/Entity/Conference.php 中,我添加了以下内容以使其commentCount可用:
public function getCommentCount(): int
{
return $this->comments->count();
}
Run Code Online (Sandbox Code Playgroud)
为了在提交评论时自动生成createdAt日期时间,我首先安装了以下包:
$ composer require stof/doctrine-extensions-bundle
Run Code Online (Sandbox Code Playgroud)
然后修改config/packages/stof_doctrine_extensions.yaml:
$ composer require stof/doctrine-extensions-bundle
Run Code Online (Sandbox Code Playgroud)
最后private $createdAt在 src/Entity/Comment.php 中使用以下内容进行装饰:
stof_doctrine_extensions:
default_locale: en_US
orm:
default:
tree: true
timestampable: true
Run Code Online (Sandbox Code Playgroud)