0
votes

I am new to symfony framework just installed and trying to run the first code.

the problem is: as mentioned in the document I created the following two files.

///home/scriptkiddie/symfony/projects/project1/src/AppBundle/Controller/MainController.php
 namespace AppBundle\Controller;
 use Symfony\Component\HttpFoundation\Response;

 class MainController
 {
   public function contactAction()
   {
     return new Response('<h1>Contact us!</h1>');
   }
 }

====================================================================

///home/scriptkiddie/symfony/projects/project1/app/config/routing.yml

app:
   resource: "@AppBundle/Controller/"
   type:     annotation

# app/config/routing.yml
contact:
    path: /contact
    defaults: { _controller: AppBundle:Main:contact }
~                                                     

Now, when I go the url http://localhost:8000/contact

I get the following error:

The autoloader expected class "AppBundle\Controller\MainController" to be defined in file "/home/scriptkiddie/symfony/projects/project1/src/AppBundle/Controller/MainController.php". The file was found but the class was not in it, the class name or namespace probably has a typo. 500 Internal Server Error - RuntimeException

1
In your routing.yml you specify "annotation" but you use yml to your routing... It's normal ?Letsrocks

1 Answers

1
votes

I think you are mixing two kind of routing definitions. First you define that the routes will be set by annotations and afterwards you are setting an annotation by yml.

You should add this annotation to your controller:

///home/scriptkiddie/symfony/projects/project1/src/AppBundle/Controller/ManController.php

namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

     class MainController
     {

      /**
       * @Route("/contact", name="contact")
       */
       public function contactAction()
       {
         return new Response('<h1>Contact us!</h1>');
       }
     }

And discard the app/config/routing.yml file.