簡體   English   中英

Symfony使用yaml和php路由

[英]Symfony using yaml and php routing

我想在Symfony(3.3.8)應用程序中混合使用yaml和php路由。 我對yaml路由非常滿意,因此我使用bin/console doctrine:generate:crud命令來查看PHP路由的外觀。 它生成了一個路由文件,看起來像

<?php

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

$collection = new RouteCollection();

$collection->add('user_index', new Route(
    '/',
    array('_controller' => 'AppBundle:User:index'),
    array(),
    array(),
    '',
    array(),
    array('GET')
));

// other CRUD routes...

return $collection;

這遵循Symfony docs上建議格式 ,您可以在RouteCollection使用Route創建一個RouteCollection ,然后將其返回。

當我嘗試使用該文件運行我的應用程序時,即使未從我的主routing.yml文件中引用該文件,也出現此錯誤:

 [Symfony\Component\Config\Exception\FileLoaderLoadException]
  The autoloader expected class "AppBundle\Resources\config\routing\restful_resource" to be defined i
  n file "/home/username/sites/appname/vendor/composer/../../src/AppBundle/Resources/config/routing/restfu
  l_resource.php". The file was found but the class was not in it, the class name or namespace probab
  ly has a typo in /home/username/sites/appname/app/config/services.yml (which is being imported from "/ho
  me/username/sites/appname/app/config/config.yml").



  [RuntimeException]
  The autoloader expected class "AppBundle\Resources\config\routing\restful_resource" to be defined i
  n file "/home/username/sites/appname/vendor/composer/../../src/AppBundle/Resources/config/routing/restfu
  l_resource.php". The file was found but the class was not in it, the class name or namespace probab
  ly has a typo.

我是否需要重新設計此文件以使其像類一樣,與Symfony文檔中建議的格式背道而馳? 還是我需要以某種方式告訴自動加載器忽略此文件,以便它不會嘗試在不應存在的類中查找該類?

Symfony 3.3中引入的一項重大更改是自動連接服務的概念。 在我不那么卑鄙的觀點中,很多不一致的魔術毫無用處。

作為自動接線過程的一部分,默認情況下決定使每個類成為一項服務。 假定任何php文件中都有一個類。 因此,嘗試在Resources / config / routing / restful_resource.php中找到一個類。

為防止此行為,您需要明確告知服務生成器跳過目錄。

// app/config/services.yml
AppBundle\:
    resource: '../../src/AppBundle/*'
    exclude: '../../src/AppBundle/{Entity,Repository,Tests,Resources}'

引入所有這些新的“幫助器”功能的好處是,我獲得了很多解釋它們的代表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM