我可以在非Symfony项目中使用Symfony的路线注释吗 [英] Can i use Symfony's Route Annotation in Non-Symfony Project
问题描述
我已经在这里尝试过 https://symfony.com/doc/master/components/routing.html 但是我做不到.
I already tried things here https://symfony.com/doc/master/components/routing.html But i couldn't make it.
<?php
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
use Composer\Autoload\ClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;
/** @var ClassLoader $loader */
$loader = require __DIR__.'/vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
$loader = new AnnotationDirectoryLoader(
new FileLocator(__DIR__.'/app/Controller/'),
new AnnotatedRouteControllerLoader(
new AnnotationReader()
)
);
$routes = $loader->load(__DIR__.'/app/Controller/');
当我这样做时,我会得到这些错误
When i do that i get these mistakes
Fatal error: Uncaught Error: Class 'Symfony\Component\Config\Loader\FileLoader' not found in C:\xampp\htdocs\MyFw\vendor\symfony\routing\Loader\AnnotationFileLoader.php:25 Stack trace: #0 C:\xampp\htdocs\MyFw\vendor\composer\ClassLoader.php(444): include() #1 C:\xampp\htdocs\MyFw\vendor\composer\ClassLoader.php(322): Composer\Autoload\includeFile('C:\\xampp\\htdocs...') #2 [internal function]: Composer\Autoload\ClassLoader->loadClass('Symfony\\Compone...') #3 C:\xampp\htdocs\MyFw\vendor\symfony\routing\Loader\AnnotationDirectoryLoader.php(23): spl_autoload_call('Symfony\\Compone...') #4 C:\xampp\htdocs\MyFw\vendor\composer\ClassLoader.php(444): include('C:\\xampp\\htdocs...') #5 C:\xampp\htdocs\MyFw\vendor\composer\ClassLoader.php(322): Composer\Autoload\includeFile('C:\\xampp\\htdocs...') #6 [internal function]: Composer\Autoload\ClassLoader->loadClass('Symfony\\Compone...') #7 C:\xampp\htdocs\MyFw\index.php(14): spl_autoload_call('Symfony\\Compone...') #8 {main} thrown in C:\xampp\htdocs\MyFw\vendor\symfony\routing\Loader\AnnotationFileLoader.php on line 25
推荐答案
我有点好奇看到在Symfony 5下这种事情确实需要什么.
I was a bit curious to see what exactly was needed for this sort of thing to work under Symfony 5.
首先加载五个软件包:
composer require symfony/routing
composer require symfony/config
composer require symfony/framework-bundle
composer require doctrine/annotations
composer require doctrine/cache
然后您需要调整composer.json才能自动加载您的应用程序类:
Then you need to tweak composer.json in order to autoload your app classes:
# composer.json
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
当然要运行"composer dump-autoload".
And of course run "composer dump-autoload".
然后我做了一个测试控制器:
I then made a test controller:
# app/Controller/DefaultController.php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController
{
/**
* @Route("/",name="index")
*/
public function index()
{
}
/**
* @Route("/hello",name="hello")
*/
public function hello()
{
}
}
此后,问题中的加载代码按预期工作.所以我不会在这里重复它.
After which, the loading code in the question worked as expected. So I won't bother repeating it here.
Symfony仍然依赖Doctrine进行某些注释处理,这有点奇怪.重要的是要注意,AnnotationRegistry :: registerLoader方法已弃用,因此,当Doctrine最终更新为3.x时,必须对代码进行调整.或者,也许教义注释依赖项将被删除.
It's a bit strange that Symfony still relies on Doctrine to do some of it's annotation processing. It's important to note that the method AnnotationRegistry::registerLoader is depreciated so the code will have to be tweaked when Doctrine is eventually updated to 3.x. Or maybe the Doctrine annotation dependencies will get removed.
另一件事是令人遗憾的是,AnnotatedRouteControllerLoader类需要加载整个Symfony框架包.该类本身仅负责设置_controller route属性.将类克隆到您自己的应用程序中,从而消除拖动完整捆绑包的必要.
The other thing that is a bit of shame is that the AnnotatedRouteControllerLoader class requires loading the entire Symfony framework bundle. The class itself just takes care of setting the _controller route attribute. It might be worthwhile to clone the class into your own application and thus eliminate the need for dragging in the complete bundle.
如果有人碰巧发现了这个答案,请参见完整的工作示例.
If anyone should happen to stumble on this answer, here is a complete working example.
这篇关于我可以在非Symfony项目中使用Symfony的路线注释吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!