marcin-jozwikowski / easy-admin-pretty-urls
Semi-automatic URL generator for pretty REST-like URLs in EasyAdmin
Installs: 15 854
Dependents: 0
Suggesters: 0
Security: 0
Stars: 31
Watchers: 2
Forks: 7
Open Issues: 0
Type:symfony-bundle
pkg:composer/marcin-jozwikowski/easy-admin-pretty-urls
Requires
- php: >=8.0
- easycorp/easyadmin-bundle: ^4.8.10 <4.14.0
- symfony/framework-bundle: ^5.4|^6.0|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9
README
Symfony Bundle that introduces customizable routes to EasyAdmin
Example
Turn
http://ea-demo.loc/en/easyadmin?crudAction=index&crudControllerFqcn=App%5CController%5CEasyAdmin%5CPostCrudController
into
http://ea-demo.loc/en/post_crud/index
Instalation
-
Install the bundle by running
composer require marcin-jozwikowski/easy-admin-pretty-urls
-
Enable the bundle by adding it to your
config/bundles.phpif not enabled automaticallyreturn [ // ... MarcinJozwikowski\EasyAdminPrettyUrls\EasyAdminPrettyUrlsBundle::class => ['all' => true], ];
-
Add a routes set pointing to a directory containing your Controllers
pretty_routes_name: resource: 'src/Controller' type: 'pretty_routes'
The
resourceis a directory path relative to your projects root directory. Type must always equal topretty_routes. See Fine-tuning / Define routes manually section to learn how this step can be ommitted.Other routing structures can be utilized as well, for example:
pretty_routes: resource: 'src/Controller' type: 'pretty_routes' prefix: /{_locale} requirements: _locale: '%app_locales%' defaults: _locale: '%locale%'
-
Make your main DashboardController extend
\MarcinJozwikowski\EasyAdminPrettyUrls\Controller\PrettyDashboardControlleror manually override the a default template like so:public function configureCrud(): Crud { return parent::configureCrud() ->overrideTemplate('layout', '@EasyAdminPrettyUrls/layout.html.twig') ->overrideTemplate('crud/field/association', '@EasyAdminPrettyUrls/crud/field/association.html.twig'); }
Configuration
The following parameters are in use:
To change the default values set the parameter in your services.yaml
parameters: easy_admin_pretty_urls.<parameter>: '<new_value>'
Or create a config/packages/easyadmin_pretty_urls.yaml file with
easy_admin_pretty_urls: <parameter>: '<new_value>'
Twig
There are one function, and one filter being registered by a Twig extension in this bundle:
pretty_urls_include_menu_index()Function returns theinclude_menu_indexvalue from Configuration|pretty_urls_remove_actionsFilter removed the unnecessary query elements from the URL string
Fine-tuning
-
Define custom URL path
By default, the URL is created as
<class_name>/<action_name>.To change that behavior specify
pathvalue inPrettyRoutesControllerattribute for the whole controller, and/or inPrettyRoutesActionattribute for the specific action.The following configuration will result in the action URL of
special/listinstead of the defaultany_fancy/index.#[PrettyRoutesController(path: 'special')] class AnyFancyController { #[PrettyRoutesAction(path: 'list')] public function index() { // .... } }
-
Putting entityID in the path
With a custom path defined in
#PrettyRoutesActionattribute, it is possible to include the entityID in the path.To achieve it, simple add
{entityId?0}to thepathargument i.e.#[PrettyRoutesAction(path: 'modify/{entityId?0}')] public function edit(AdminContext $context) { return parent::edit($context); }
Due to the way form actions are generated by EasyAdmin it is necessary to specify a default value for the parameter in the path.
It does not have to be
0but keep in mind that setting it to an existing ID will make the application unusable. -
Select actions to create routes for
By default, pretty routes are generated for
index,new,detail,edit,delete,batchDelete,renderFilters, andautocompleteactions.To change them globally set a proper configuration value. For a single-controller change add a
PrettyRoutesControllerattribute to the controller and name the actions you want to have pretty routes for, inactionsparameter.#[PrettyRoutesController(actions: ['index', 'foo', 'bar'])] class AnyFancyController { // ... } /** * You can also (optionally) specify your own dashboard controller if you're using more than one in your project. * This will avoid to have an incorrect sidebar/user menu by addressing the request to the right dashboard controller */ #[PrettyRoutesController(actions: ['index', 'foo', 'bar'], dasboard: YourCrudController::class . '::yourCrudAction')] class AnyFancyController { // ... }
You can also just add your custom actions to the default list by specifying
customActionsinPrettyRoutesControllerattribute.#[PrettyRoutesController(customActions: ['foo', 'bar'])] class AnyFancyController { ...
-
Define routes manually
Instead of defining a
pretty_routesroutes to automatically parse all classes in a directory you can create routes that will replace your default EasyAdmin CRUD actions.pretty_foobar_index: path: /foobar-url controller: \App\Controller\EasyAdmin\DashboardController::index defaults: crudControllerFqcn: \App\Controller\FoobarCrudController crudAction: index
controllervalue must point to your projects DashboardControllerdefaultscrudControllerFqcnandcrudActionmust point to your target CRUD controller and its action.pathcan be anything of your choosing- Route name must match the pattern
<prefix>_<name>_<action>with<action>equal tocrudActionvalue from the defaults<name>being the target controller class name (not FQCN - just the last part) stripped ofController, written in snake_case<prefix>is set toprettyby default. See Configuration to ways to change it.
- When routes are defined manually the Installation step 3 is not required.
You can generate a YAML routes configuration for existing controllers for further manual modifications by running
bin/console pretty-routes:dump <resource>
Troubleshooting
-
Routes not working
If your routes are still not generated despite being added, check your logs for
'Pretty route not found'withdebuglevel. Those will list all the EasyAdmin routes that did not have their pretty counterparts.Most probably, there's some naming mismatch.
-
Checking the Resource parsing results
To see what is the outcome of parsing a
pretty_routesResource run the following command:bin/console pretty-routes:debug <resource>