I want to make a sitemap in symfony 6 but I don't know how to do the routing.
symfony 5.
/**
* #Route("/sitemap.xml", name="sitemap", **defaults={"_format"="xml"**})
*/
Symfony 6.
#[Route('/sitemap', name: 'app_sitemap', ?...)]
thanks you.
The Route class has various parameters to the constructor, such as default: ['_format' => 'xml'].
The https://github.com/prestaconcept/PrestaSitemapBundle May also greatly assist.
Related
I am trying to understand how Symfony 5 routing works.
What will happen if in my project I have 2 different controllers with the same route name?
For example, in Controller1.php:
/**
* #Route("/publisher/notification_update/")
*/
And in Controller2.php:
/**
* #Route("/publisher/notification_update/")
*/
Then from somewhere inside a twig, I have a form that calls this route as follows:
<form name="notification" id="notification{{message.notification_id}}"
action="/publisher/notification_update/" style="display:none"
method="post">
<input hidden class="hiddenNotificationsForUser" type="text" id="{{message.notification_id}}"
value="{{message.message}}" onclick="this.form.submit();" />
</form>
What will happen in this case? Will there be an error, or will it go to the first route it finds alphabetically?
In symfony 5 I have used #[Route(['/login', 'admin/login'], name: 'app_user_login')] for route two url paths for single controller action
After reproducing this scenario with Symfony 5 on a LEMP server in DigitalOcean, I found out that there is no error or warning message.
Symfony picked the route specified in Controller1.php:
/**
* #Route("/publisher/notification_update/")
*/
This, however, is a bad thing because having the situation I described combined with a large code base can lead to a very hard to identify bug.
You may never reach the route that you need even if everything is correct with it's contents.
I'm having some trouble runnning Symfony. In fact, it can't find the default twig template. I didn't touch the code at all, juste generated my bundle and trying to access /web/app_dev.php.
My template is in
/src/OC/PlatformBundle/Resources/views/Default/index.html.twig.
Symfony looked into these locations, but not where my template actually is.
/app/Resources/views
/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form
And the DefaultController.php indexAction() looks ok :
public function indexAction(){
return $this->render("OCPlatform:Default:index.html.twig");
}
If any of you have ever faced this kind of issue or have any idea where the problem comes from, I thank you in advance for your help.
Arthur
I've same problem and i resolve it with this route :
public function indexAction(){
return $this->render("#OCPlatform/Default/index.html.twig");
}
Edit ":" with "/" and it work.
Maybe can help other developper
If you want to keep using the 'OCPlatform:Default:index.html.twig' format for templates then edit your app/config/config.yml file:
#app/config/config.yml
framework:
templating:
engines: ['twig']
This fixed my problem. You can check this link
Symfony 3.4 Use view inside my bundle
Following the docs(Symfony >=3.4) you should write the path to the template in the controller like this:
public function indexAction(){
return $this->render("#OCPlatform:Default:index.html.twig");
}
This worked for me:
return $this->render('#HomeBundle/Default/index.html.twig');
Hope it hope :)
Referencing Templates in a Bundle¶
If you need to refer to a template that lives in a bundle, Symfony uses the Twig namespaced syntax (#BundleName/directory/filename.html.twig). This allows for several types of templates, each which lives in a specific location:
#AcmeBlog/Blog/index.html.twig: <br>This syntax is used to specify a template for a specific page. The three parts of the string, each separated by a slash (/), mean the following:<br>
#AcmeBlog: is the bundle name without the Bundle suffix. This template lives in the AcmeBlogBundle (e.g. src/Acme/BlogBundle);<br>
Blog: (directory) indicates that the template lives inside the Blog subdirectory of Resources/views/;<br>
index.html.twig: (filename) the actual name of the file is index.html.twig.
In symfony 4 use this :
return $this->render("#Platform/Default/index.html.twig");
The Cookbook show how to translate assert messages, but how to do the same on #Constraint annotations on a class level? Example which is not working :
use Symfony\Bridge\Doctrine\Validator\Constraints as Constraint;
/**
* #Constraint\UniqueEntity(
* fields={"mobileDomainName"},
* errorPath="mobileDomainName",
* message="site.validation.mobileName.alreadyUsed"
* )
*/
class Site {
Thanks for your answers.
EDIT : translator is activated, all others classical calls worked (including Assert annotations).
In my case I have to enable translator in the config
translator: {
fallbacks: ['%locale%']
}
like Adam Elsodaney said.
Thanks to A.L comment, I read again the documentation (that I already tried to follow). But I misread the translation file name (i.e. validators.language.filetype), whereas I orignally dropped my translations in my custom validation file.
With all constraints validation in good file, all is working.
Thanks for your answers and pointing.
I would like to overwrite default routing of FOSUserBundle for Symfony2 to get a result like this:
www.mysite.com/en/login
www.mysite.com/es/login
www.mysite.com/fr/login
www.mysite.com/en/profile
www.mysite.com/es/profile
www.mysite.com/fr/profile
Thanks for your help.
Look at this link. I think it is exactly what you need.
[...] to add a prefix before the _locale string (e.g. /admin/en/dashboard), you can add the “i18n_prefix” option.
# app/config/routing.yml
dashboard:
...
options: { i18n_prefix: admin }
I've been working with Symfony2 recently, and I've been using annotations for routing, instead of the standard yml or PHP.
I've managed to get my head around most of it, but I'm having trouble using #Method to limit what type of http requests get a response from a controller.
I'm currently using:
/**
* #Route("/contact/", name="_contact")
* #Method({"GET", "POST"})
*/
and getting an error that states Cannot import resource "C:\wamp\www\jimmy\src\Scott\BlogBundle/Controller/" from "C:/wamp/www/jimmy/app/config\routing.yml".
If I remove the #Method line completely the page loads as normal. I've read the documentation on symfony about #Method and they do the same #Method line in their examples, so I'm really not sure where I have gone wrong.
My routing.yml in app/config is:
scott_blog:
resource: "#ScottBlogBundle/Controller/"
type: annotation
prefix: /
Thanks for any help.
You might have forgot to use the namespace Sensio\Bundle\FrameworkExtraBundle\Configuration\Method . If that is the case , add
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;