0
votes

On the routing page of smyfony is an routing example (the first one). Now they give us 4 options of code (Annotations, YAML, XML, PHP). Where is the difference? And maybe you can take a look on my Controller + Route.

The controller looks like this:

<?php
    namespace AppBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

    class ToDoListController extends Controller
    {
        /**
         * @Route("/ToDoList", name="ToDoList")
         */

        public function showToDoList()
        {

        }
    }
?>

Now I added this route into the routing.yml too.

ToDoList:
    path: /Aufgabenliste
    defaults: {_controller: AppBundle\Controller  \ToDoListController::showToDoList}

Is that correct? Whats about the path? In the first example of the symfony page they wrote defaults: { _controller: AppBundle:Blog:show } but in the description they wrote:

The _controller string is called the logical name. It follows a pattern that points to a specific PHP class and method, in this case the AppBundle\Controller\BlogController::listAction and AppBundle\Controller\BlogController::showAction methods.

1
@keyboardSmasher thanks! I Is my entry inside the routing.yml correct? My ToDoListController is locadet in \src\AppBundle\Controller\ToDoListController.phpMyNewName
With annotations you just have to point to each bundle's Controller directory and Symfony will automatically load all of your annotation routes. You should just copy/paste the code at the link I provided above.keyboardSmasher
@keyboardSmasher ah okay. Thanks for your help!MyNewName

1 Answers

1
votes

Allright. So in Symfony you get to choose between different methods of routing. You can do this for example via annotations or yml(I used to use yml but now I switched to annotations). The only difference is in... format of the file ;) For example I realy like to use annotations because instead of having one billion entries in one file I have every route right next to the code it leads to. At this point for you it depends on what you like better. However I think it's considered as good practice to use annotations.

<?php
    namespace AppBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

    class ToDoListController extends Controller
    {
        /**
         * @Route("/ToDoList", name="ToDoList")
         */
         public function showToDoList()
         {

         }
    }  
?>

Code above is ok - you used required namespaces and created correct annotation. All of this you have to do in your bundle you are working on. Also you have to go to app/config/routing.yml and put there something like this:

YourBundleName:
    resource: "@YouBundle/Controller/"
    type:     annotation

Of course it's just an example - you need to adjust it to your needs. This way you are saying to Symfony that you want to use annotations.