#Method breaking routing - symfony

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;

Related

Symfony internalization routes with params

I am developing a project in Symfony 4.3. I am trying to create URLs in different languages. I have tried to use this notation:
/**
* #Route({
* "it": "/{_locale}/rassegna/stampa",
* "bg": "/{_locale}/Статии/в/печата"
* }, name="rassegna_stampa", utf8=true)
*/
The problem is that when I try to create a link in a twig to change language, for example:
GO TO BG LANGUAGE
it is rendered in something like:
http://website.it/it/%D0%A1%D1%82%D0%B0%D1%82%D0%B8%D0%B8/%D0%B2/%D0%BF%D0%B5%D1%87%D0%B0%D1%82%D0%B0
So it seems that the translated final part is ok, but the _locale parameter is not matched, as the {_locale} placeholder is filled with the current locale, and not with the locale that I requested in the link. It is 'it' but it should be 'bg'.

Symfony 4 : setting {_locale} parameter in route definition leads to "no route found" error

I've a Symfony 4 app that is running fine until I want to define a route with the _locale parameter.
I define two routes for the same action with the following code :
/**
* #Route("/sites/{id}",
* name="site_get",
* methods={"GET"},
* requirements={"id": "\d+"},
* )
* #Route("/{_locale}/sites/{id}",
* name="site_get_with_locale",
* methods={"GET"},
* requirements={"id": "\d+"},
* )
*/
When I call the url /sites/1 I get the resource but if I call /en/sites/1 I get a 404 error "route not found".
Here are the things I've tried :
tried to change /{_locale}/sites/{id} with /{locale}/sites/{id} and the route is working but, obviously, the locale is not changed.
tried to set requirements for the _locale parameter but it is no better
tried to set the {_locale} parameter as a prefix (in annotations.yaml) with no luck neither
I can see the route definition in the debug:router :
site_get.0 GET ANY ANY /api/sites/{id}
site_get_with_locale.0 GET ANY ANY /api/{_locale}/sites/{id}
Edit 1 :
In the profiler for the given failed request /en/sites/1 :
the configuration sections shows me thet the Intl locale is fr.
In the routing section I don't see the same at in the debug:router command :
{_locale} is changed into 0 (even if I set requirement for the _locale to fr|en)
Edit 2 :
I was using Symfony 4.4.8 but downgrading to 4.4.7 seems to work.
I also have the problem with 4.4.10
Any idea of another path to explore?
Thank you,
Vincent

Is it possible to make Symfony ignore a double slash in the URL

I'm writing a new endpoint for 3rd party desktop application, and there are several different functions in the application that post to the same endpoint on my Symfony 2.8 server.
Sometimes the desktop application goes to the correct path - example.com/path/to/endpoint. However sometimes it tries to go to add an extra slash in between the domain name and the path - example.com//path/to/endpoint.
I tried to just add an extra route with the double slash in it like this:
/**
* #Route("/path/to/route", name="example_route")
* #Route("//path/to/route", name="example_route_double_slash")
* #Method({"POST"})
*/
Symfony just ignores the double slash when it compiles the routes though, and I end up with 2 of "/path/to/route" if I check my routes with app/console debug:router
By default, the Symfony Routing component requires that the parameters
match the following regex path: [^/]+. This means that all characters
are allowed except /.
You must explicitly allow / to be part of your parameter by specifying
a more permissive regex path.
Your route definition have to use regex :
/**
* #Route("/{slash}/path/to/route", name="example_route_double_slash", requirements={"slash"="\/?"})
*/
I haven't tested the code but it basically says that you are adding an extra parameter that may or may not be a /.
The code snippet is inspired from this one at official docs :
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class DemoController
{
/**
* #Route("/hello/{username}", name="_hello", requirements={"username"=".+"})
*/
public function helloAction($username)
{
// ...
}
}
find more here : http://symfony.com/doc/current/routing/slash_in_parameter.html

How to translate #Constraint messages in Symfony 2.8?

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.

Symfony 2.1 - FPNTagBundle with annotated Tag entity

I have installed FPNTagBundle via composer. I have successfully added tags to my articles, but when I add DoctrineExtensions' TagRepository as the repositoryClass with
/**
* #ORM\Entity(repositoryClass="DoctrineExtensions\Taggable\Entity\TagRepository")
* #ORM\Table
*/
class Tag extends FPN\TagBundle\Entity\Tag
and try to use TagRepository's method getResourceIdsForTag('post', 'tagname'), it fails. I've checked the repository class with
$tagRepo = $this->getDoctrine()->getRepository('GergelyPolonkaiFrontBundle:Tag');
echo get_class($tagRepo);
and it's not TagRepository, but Doctrine\ORM\Entity\Repository. I don't have any other error message regarding the repositoryClass thing, even if I specify an invalid classname.
Have I found a bug, or do I miss something somewhere?
It seems I was hitting a bug. After merging some pull requests by Fabien Pennequin 16 days ago, it now seems working as expected.

Resources