I'm using latest version of PHPStorm 10 and want to use annotations for my Symfony 2.8.1 routes. I want to use PHPStorm's autocomplete feature to autocomplete my routes in twig files, but they are autocompleted wrong. I'm using both Symfony2 plugin and PHP Annotations.
Routing file
#app/config/routing.yml
ParkResortBundle:
resource: "#ParkResortBundle/Controller"
prefix: /
type: annotation
My controller with one route
namespace ParkResortBundle\Controller;
class DefaultController extends Controller
{
/**
* #return Response
* #Route("/")
*/
public function indexAction()
{
return $this->render('ParkResortBundle:Pages:firstpage.html.twig');
}
}
This should normally generate park_resort_default_index but instead it generates parkresort_default_index and my PHPStorm finds and autocompletes it with the underscore. I've also ran debug:router to confirm and it indeed finds the route and it does work without the underscore. But I want it with the underscore.
Even in the docs it says:
sensio_blog_post_index is the route for SensioBlogBundle's Postcontroller index action. It puts an underscore in between the camelcase capital letters.
What did I do wrong?
I will add your cases. see external issues
https://github.com/Haehnchen/idea-php-symfony2-plugin/issues/673
EDIT:
fixed in version "0.11.109"
Related
I was reviewing the symfony 3.4 routing page. I have a misunderstanding if anyone can help me. So say you have the following:
In Your controller:
class BlogController extends Controller
{
/**
* Matches /blog exactly
*
* #Route("/blog", name="blog_list")
*/
public function listAction()
{
// ...
}
}
And in your routing.yml:
blog_list:
path: /blog
defaults: { _controller: AppBundle:Blog:list }
Would you be able to delete the route annotation above the function. Because now the routing is being handled by the routing.yml?
Many thanks
You must choose one of this methods to set route for current url "/blog"
In Symfony Routing can be declare using YAML, XML, PHP or annotation. It is recommended you stick with only one but you can use multiple approaches in a single project.
Official doc for routing Symfony Routing
and the answer to your question is, I would say yes you could delete the annotation.
Because now the routing is being handled by the routing.yml
for the big project, I prefer YML routing.
I am starting to play with symfony4. I've just created new application and create new LuckyController. It works with routes.yaml configured in this manner:
lucky:
path: /lucky/number
controller: App\Controller\LuckyController::number
With the following controller:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class LuckyController
{
public function number()
{
return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
}
}
But I want to use annotations. So I decided to comment routes.yaml. Following documentation that explain how to create a route in symfony I've made this:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function number()
{
return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
}
}
Sometimes this problem occurs because of cache.you need to run php bin/console cache:clear.then it will work fine.
In Symfony4 you have to install annotations bundle.
Run this command composer require annotations
Then restart Your project.
I had the same thing when trying the annotations, then I found out with the demo installed (the blog) you need to add the language in the URL. So the documentation says:
http://localhost:8000/lucky/number will work exactly like before
That did not work. This however did:
http://localhost:8000/en/lucky/number
As suggested in #Jack70 answer.
https://127.0.0.1:8000/en/random/number
This should work.
If you run the command php bin/console debug:router it would show you the list of routes available as you can see in that list, you need to add the locale (language) as first argument.
app_annotationsample_number ANY ANY ANY /{_locale}/random/number
If you use annotations - you need check config/routes/anotations.yaml.
You need check path to your controller or add new path, for example:
controllers:
resource: ../../src/Controller/
type: annotation
Also, you'll get that error if the route name appears more than once.
I ran into that error while developing Angular app, so in order to debug I tried that same route in Postman, which I knew used to work fine, but this time it did not throwing the mentioned error. Luckily, I have just added one new controller, so finding the culprit was easy: the problem was that after copying methods from web controller to api controller I forgot to change route name in one of the methods.
So you might wanna be careful when copying :)
I've got a question about the inheritance of annotations in symfony.
Class A:
abstract class AController
{
/**
* Test action
* #Route("/", name="test")
* #Template("...")
*/
public function testAction()
{
...
}
}
My question is if it is possible from Class B not only to inherit the function but also the route annotation. Or what a nice workaround would be. Something like:
class BController extend AbstractController
{
public function testAction(){
return parent::testAction();
}
}
I was looking for a similar solution for an admin subdomain on a project I'm working on. First, I tried to use your method and realized it isn't supported. So I set updated my configuration instead. The following configuration automatically restricts all routes in the /src/Controller/Admin directory to my admin subdomain and prefixes all the route names with "admin_".
# /config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/**/*
type: annotation
exclude: ../../src/Controller/Admin
admin:
resource: ../../src/Controller/Admin/
type: annotation
host: admin.%site_name%
name_prefix: admin_
....
There's a couple things to note here. First, "%site_name%" is a parameter defined in my project. Second, there seems to be a bug with the exclude option on route configurations where it only works if you use a glob pattern for resource option. Hence the "../../src/Controller/**/*".
This question is pretty old, but hopefully this helps someone.
I'm going to create a modules system in my Symfony 2 app. Each module will be a bundle.
I don't know how to I can dynamically (in my service code) load routes from file (eg. AcmeSomeModuleBundle/Resources/config/routing.yml) and apply them with some prefix (or host). Like it's done by embedding code below in app/config/routing.yml:
berg_applications:
resource: "#AcmeSomeModuleBundle/Resources/config/routing.yml"
host: foobar.com
Any solutions?
You need custom route loader IMO: http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html
For one project, I had to build route loader which loaded routes by fetching them from remote URL via CURL and it worked perfectly.
Documentation is very clear and it's silly easy to build one yourself when you look at the example. Basically, key things are:
"type" when you're defining a route resource. You should make your custom type so that your route loader recognizes it and takes it for processing.
::load() method.
If you have any concrete problems you stumble upon don't hesitate to post question in comment. Basically, your RouteLoader will receive "resource" in it's load method and should do whatever it needs to do with it to add new Route to Router.
If you do a true bundle approach for each module, then the easiest way to accomplish what your trying to do is use the JMS Security-Extra bundle with attribute-based routing.
To your composer.json file, add this:
"require": {
...
"jms/security-extra-bundle": "1.5.*",
Update your composer file with
php composer.phar update
Then in your BundleName/Resources/config/routing.yml file do this:
some_name:
type: annotation
resource: "#SomeBundle/Controller"
Finally, for each action in your controller, decorate it with #Route attributes:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
/**
* #Route("/SomeBundle/SomeController")
*/
class SomeController extends Controller {
/**
* #Route("someAction", name="myAction")
* #Method("GET") OR
* #Method({"GET", "POST"})
*/
public function someAction() {
}
}
Some of the other attributes in the JMS bundle make things really nice as well. For example, I use the #Template attribute on my actions quite a bit. This means that I no longer have to do:
public function recentListAction() {
...
return $this->render(
'AcmeArticleBundle:Article:recentList.html.twig',
array('articles' => $articles)
);
}
I can simply do:
/**
* #Route("/Articles/List")
* #Template()
*/
public function recentListAction() {
...
return array('articles' => $articles);
}
And as long as I have a Resources/views/ControllerName/recentList.html.twig file, everything will be weaved together for me automatically.
I just downloaded symfony2 and am beginning to play with routing via annotations. I have my app/config/routing.yml set in the bundle I created to use annotations, and have deleted the Acme bundle and all routing references to it. That said, I've tried creating a couple different route annotations in my controller like #Route("/") and #Route("/hello/{name}") but I'm always greeted with a 404 error (using the dev environment). If I add the route in routing.yml it works just fine even though the routing is configured to use annotations. For whatever reason, my annotations are seemingly being ignored.
Here is my app/config/routing.yml:
DanDefaultBundle:
resource: "#DanDefaultBundle/Controller/"
type: annotation
prefix: /
And here is my controller method:
/**
* #Route("/")
* #Template()
*/
public function indexAction()
{
return array('name' => 123);
}
I've included the Sensio\Bundle\FrameworkExtraBundle\Configuration\Route namespace - everything as far as I can tell is correct with what I've seen in the documentation. What am I overlooking that's causing symfony2 to seemingly ignore my routing annotations? Again, if I add the routes to the routing yaml everything works so my bundle is working - but annotations seem to be ignored.
Thanks!
Dan
UPDATE:
It looks like I had to add the routes to routing_dev.yml in addition to routing.yml since I was operating in the dev environment. I suppose that's so you have have different routes in-between development and production? I suppose special care will have to be taken to make sure those routes stay in sync?
You accidentally removed the inclusion of routing.yml from routing_dev.yml.
if you use Route Prefix in your routing.yml
you must declare about your prefix above your class declaration like that :
/**
* #Route("/")
*/
class PostController extends Controller
{
/**
* #Route("/")
* #Template()
*/
public function indexAction()
{
}
/**
* #Route("{id}")
* #Template()
*/
public function showAction($id)
{
}
}
Like into Sensio FrameworkExtra Bundle Documentation