2017-10-12 188 views
0

要我Symfony的版本在app/config/routing.yml文件3.3的項目,我已經把:Symfony的3.3:內包加載路由使用PHP文件需要

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

而且在src/AppBundle/Resources/config/routing.php我已經把以下內容:

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; 

,但我得到了以下錯誤:

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").

你知道夥伴們如何通過「外部」php文件加載路線嗎?我的意思是,通過yml加載通過php加載它們的路徑。

正如我所知,根據:http://symfony.com/doc/current/routing.html你可以使用PHP文件來加載路由。

+0

Symfony的版本?你有什麼看起來應該工作。 – Cerad

+0

它在標題 –

+0

上寫着我忘了我已經回答了這個問題:https://stackoverflow.com/questions/46099282/symfony-using-yaml-and-php-routing/46100447#46100447 – Cerad

回答

-1

可能是因爲您的routing.php文件中沒有命名空間。

你必須把在文件的開頭:

namespace AppBundle\Resources\config; 
+0

不是仍然要求上課。 –

+0

也許你只能在默認文件(app/config/routing.yml | php)中配置php路由,並且在php中添加其他路由的唯一解決方案是創建一個[自定義路由加載器](https:// symfony .com/doc/current/routing/custom_route_loader.html) –

+0

@FabienSalles - 面臨嚴峻的風險,也許你應該停止猜測並開始閱讀文檔並做一些測試。 – Cerad