Symfony 3.3: Use php file inside bundle to load routes expects class

时间:2017-10-12 10:03:41

标签: php symfony symfony-3.3 symfony-routing

To my Symfony version 3.3 project at app/config/routing.yml file I have put:

AppBundle:
    resource: '@AppBundle/Resources/config/routing.php'
    prefix: /
    type: 'php'

And on src/AppBundle/Resources/config/routing.php I have put the following:

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$collection = new RouteCollection();
$collection->add('blog_list', new Route('/blog', array(
    '_controller' => 'AppBundle:Blog:list',
)));
$collection->add('blog_show', new Route('/blog/{slug}', array(
    '_controller' => 'AppBundle:Blog:show',
)));

return $collection;

But I get the following error:

The autoloader expected class "AppBundle\Resources\config\routing" to be defined in file "/home/pcmagas/Kwdikas/myblog/vendor/composer/../../src/AppBundle/Resources/config/routing.php". The file was found but the class was not in it, the class name or namespace probably has a typo in /home/pcmagas/Kwdikas/myblog/app/config/services.yml (which is being imported from "/home/pcmagas/Kwdikas/myblog/app/config/config.yml").

Do you know fellows how to load the routes via a "external" php file? I mean in the very same way you load via yml the routes to load them via php.

As fas as I know according to: http://symfony.com/doc/current/routing.html you can use php file to load the routes.

1 个答案:

答案 0 :(得分:-1)

May be its because you don't have a namespace in your routing.php file.

You have to put at the beginning of the file :

namespace AppBundle\Resources\config;