我正在嘗試在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").