symfony2 中的服务 - 服务文件应该是什么样的?
我正在尝试在 symfony2
中创建服务,它将验证会话是否包含某些信息,如果不将用户重定向到另一个控制器.我希望这段代码作为服务工作,因为我将在许多控制器中使用它.
I am trying to create service in symfony2
which will verify if session contains certain information and if not redirect the user to another controller. I want this piece of code to work as a service as I will be using it in many controllers.
我有问题,因为 Symfony2 手册没有提供服务文件应该是什么样子的信息.应该是普通的php类吗?
I have problem as manual on Symfony2 book does not provide information how service file should look like. Should it be a normal php class?
请在下面找到我的文件转储,以及我收到的错误信息.
Please find below dump of my files with information on error that I receive.
在 \AppBundle\Services
我创建文件 my_isbookchosencheck.php
包含:
In \AppBundle\Services
I create file my_isbookchosencheck.php
containing:
<?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
我的控制器文件:
/**
* 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").
你犯的错误很少,我将简要解释一下,我会给你一个你想要创建的服务的例子.
There are few mistakes that you made, which I am going to explain in short, and I will give you an example of the service you want to create.
您在
AppBundle\Services
中创建了您的服务,但您的命名空间注册方式不同 -命名空间 AppBundle\Services\my_isbookchosencheck;
.它应该是namespace AppBundle\Services;
.我还建议您在创建目录时使用单数名称 - 在这种情况下,Service
会更好,而不是Service
.
You created your service in
AppBundle\Services
, yet your namespace is registered differently -namespace AppBundle\Services\my_isbookchosencheck;
. It should benamespace AppBundle\Services;
. I would also advise you to use singular names when creating directories - in this caseService
would be better, instead ofServices
.
您正在直接使用 __constructor
来应用一些逻辑并返回它的结果.更好的方法是创建一个自定义方法,可以在必要时访问它.
You're using your __constructor
directly to apply some logic and return the result of it. Better way would be to create a custom method, which could be accessed when necessary.
您正在创建 Session
的新实例,这意味着您将无法访问先前添加并存储在会话中的任何内容.正确的方法是注入保存当前 Request
的 RequestStack
并从那里获取会话.
You're creating new instance of Session
which means that you wont be able to access anything that was previously added and stored in session. The right way here, would be to inject RequestStack
which holds the current Request
and get the session from there.
我相信您也注册了错误的服务.在您的 services.yml
文件中,它应该位于 services:
选项下.这就是您粘贴错误的原因.
I believe you also registered your service wrong. In your services.yml
file, it should be under services:
option. This is why you got the error you pasted.
那么,让我们看看您的服务应该是什么样子.
So, let's see how your service should like.
services.yml
services:
book_service:
class: AppBundle\Service\BookService
arguments:
- @request_stack
- @router
BookService.php
namespace AppBundle\Service;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
class BookService {
/* @var $request Request */
private $request;
/* @var $router RouterInterface */
private $router;
public function __construct(RequestStack $requestStack, RouterInterface $router) {
$this->request = $requestStack->getCurrentRequest();
$this->router = $router;
}
public function isBookChoosen() {
$session = $this->request->getSession();
// Now you can access session the proper way.
// If anything was added in session from your controller
// you can access it here as well.
// Apply your logic here and use $this->router->generate()
}
}
现在在您的控制器中,您可以像这样简单地使用它:
Now in your controller you can simply use it like this:
$this->get('book_service')->isBookChoosen()
这是一个简短的例子,但我希望你能明白.
Well this is a short example, but I hope you got the idea.