我正在尝试在symfony2
中创建服务,它将验证会话是否包含某些信息,是否将用户重定向到其他控制器。我想要这段代码作为服务工作,因为我将在许多控制器中使用它。symfony2中的服务 - 服务文件的外观应该如何?
我有问题,因为Symfony2手册上没有提供信息如何看待服务文件。它应该是一个正常的PHP类吗?
请在下面找到我的文件的转储与我收到的错误信息。
在\AppBundle\Services
我创建一个包含文件my_isbookchosencheck.php
:
<?php
namespace AppBundle\my_isbookchosencheck;
class my_isbookchosencheck
{
public function __construct();
{
$session = new Session();
$session->getFlashBag()->add('msg', 'No book choosen. Redirected to proper form');
if(!$session->get("App_Books_Chosen_Lp")) return new RedirectResponse($this->generateUrl('app_listbooks'));
}
}
我service.yml
:
my_isbookchosencheck:
class: AppBundle\Services\my_isbookchosencheck
我conntroller文件:
/**
* This code is aimed at checking if the book is choseen and therefore whether any further works may be carried out
*/
$checker = $this->get('my_isbookchosencheck');
错误:
FileLoaderLoadException in FileLoader.php line 125: There is no extension able to load the configuration for "my_isbookchosencheck" (in C:/wamp/www/symfony_learn/app/config\services.yml). Looked for namespace "my_isbookchosencheck", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "fos_user", "knp_paginator", "genemu_form", "debug", "acme_demo", "web_profiler", "sensio_distribution" in C:/wamp/www/symfony_learn/app/config\services.yml (which is being imported from "C:/wamp/www/symfony_learn/app/config\config.yml").