Drupal 9 module development - custom route using wildcard as URL prefix - drupal

I need to build the following custom route pattern:
{prefix}/test/
I'm using this base module as example (https://github.com/sidharrell/D8HWexample), and already did the d9 updates.
At this moment my module's .routing.yml looks like this:
hello.content:
path: '{region}/hello'
defaults:
_controller: '\Drupal\testroute\Controller\HelloController::content'
_title: 'Hello World'
requirements:
_permission: 'access content'
But once I clear the cache, any url like /asd/hello, will fallback on 404.
When I use the wildcard as suffix, it works as expected, but I need it as an URL prefix.
I know there is a way using Event and Route Subscriber, but I'm looking for the most simple solution for that.
Any idea will be much appreciated. Please let me know if more info is needed.
PS: adding a slash before the path still returns me 404.
Example:
path: '/{region}/hello'

Related

How to secure Lexik Translation Bundle routes?

I am using Symfony 2.8 and I import my translations into the database with the help of the Lexik translation bundle - this allows me to have a route like /admin/translations where i can see statistics and add more translations (well, the customer will).
The problem is that this route has no security at all. I can just access it without even being logged in. I have an annotation that makes sure you have a certain role in order to access the given route.
My question is how can I add my custom annotation to the lexik controller that renders the translation templates (like overview and grid pages) without actually writing code inside the bundle (vendor folder). Is there a way to add it in the config?
In your security.yml you just need to add access control on the admin path
access_control:
- { path: ^/admin, role: ROLE_ADMIN }

Different path to same routing file from main routing file in Symfony2

I have 7 bundles in my project. So I pointed each from main routing file (app->config->routing.yml).
I have a requirement that same routing file inside a bundle can able to be called using different paths. Like one path with an argument and other without argument.
My main routing file looks like as follows
clubadmin_general:
resource: "#TestGeneralBundle/Resources/config/routing.yml"
host: "mywebsite"
prefix: /{url_identifier}/admin/
clubadmin_general_club_domain:
resource: "#TestGeneralBundle/Resources/config/routing.yml"
prefix: /admin/
The routing file inside the bundle is common when called form main routing file. The routing file inside the bundle is looks like..
show_dashboard:
pattern: /
defaults: { _controller: TestGeneralBundle:Dashboard:index }
When I called, routing defined first gives "No route found". But the second works well. When I changed the order, always second works well.
In the above code path "/admin/" works well. But path "/{url_identifier}/admin/" will not work.
I think "show_dashboard" name is cached for a route. May be that is the issue. But couldn't find a solution.
Someone please help me.
Finally found a solution... By create different environment, the cache folder get separated and then can able to use same routing names with different paths.

How to verify (google) domain ownership with html file method in Symfony?

How to add an html file to prouve ownership of a domaine in a Symfony application ?
Here is my working solution but I think it's pretty ugly because :
I have to create a special route
I have to store the file outside of /web directory where I think it is supposed to live.
Step 1 - Create a route (src/mybundle/Ressources/config/routing.xml
my_route_name:
path: /google43d423494a0981c2.html
defaults: { _controller: MyBundle:Main:googleCheck}
Step 2 - Create a controler action
public function googleCheckAction(){
return $this->render('google43d423494a0981c2.html.twig');
}
/!\ Carreful - trick 1 you need to rename the file with the .twig extention otherwise this won't work.
Now put the file under app/Resources/views/
You can also put the file in any of your bundle views, just change the path in the controler action.
What would be the best way to handle this case ?
Three options:
Just put a HTML file in the web/ folder and let the web server render it.
If necessary, you can store it outside of the Symfony project, and just tell your web server where to find it.
Alternatively, create a route in routing.yml and [serve it using the default controller] (http://symfony.com/doc/current/cookbook/templating/render_without_controller.html) and store the template in the root of app/Resources/views.
gwt:
path: /google43d423494a0981c2.html
defaults:
_controller: FrameworkBundle:Template:template
template: '::google43d423494a0981c2.html.twig'
I personally would choose option #2, because I wouldn't want to add a file which had nothing to do with the application into the repository.

Symfony2: Add a prefix route to a bundle

I am working with Symfony2 and I have a bundle that includes multiple controllers. I want to have a common prefix route for all of them.
Something like this:
bundle_path/controller1_path
bundle_path/controller2_path
bundle_path/controller3_path
etc.
Is there a way to do this without writing bundle_path into every single controller?
You can add a prefix to alle the bundle's route in the routing.yml files. As Example:
acme_customer_area_frontend:
resource: "#AcmeCustomerAreaFrontendBundle/Resources/config/routing.yml"
prefix: /customer/frontend
Hope this help

Change default path on Symfony CMF

I installed Symfony CMF structure bundles on my project, but default route is on
localhost/project/web/app_dev.php/
and i want to show it on
localhost/project/web/app_dev.php/cms
Thank you!!
So you do want to install symfony at the webroot, right? If that is the case, your best bet is to add a route /cms and add everything else under that route, instead of directly to the root. When using the RoutingAutoBundle, this should be no problem to configure. If your editors create routes themselves, you would need to work with the widget for selecting the parent of the routes to only propose the cms/ route you created - unless you think it could make sense for them to occasionally place a route outside of /cms.
If you want to be more strict, i think your best bet is the upcoming ResourceBundle. Discuss this with Dan over at https://groups.google.com/forum/#!topic/symfony-cmf-devs/6Wha2o3qHPE
Not sure if I understood your question. If you want your default home page at localhost/project/web/app_dev.php/ pointing to your cmf bundle, just add this at the begining of app/config/routing.yml
homepage:
pattern: /
defaults: { _controller: YourCMFBundle:Welcome:index }
Where Welcome is the name of your CMF default controller.

Resources