symfony2中的服务-服务文件应该是什么样子?
我正在尝试在symfony2中的服务-服务文件应该是什么样子?,symfony,Symfony,我正在尝试在symfony2中创建服务,该服务将验证会话是否包含某些信息,如果没有,则将用户重定向到另一个控制器。我希望这段代码作为一项服务来工作,因为我将在许多控制器中使用它 我有问题,因为Symfony2手册并没有提供服务文件应该是什么样子的信息。它应该是一个普通的php类吗 请在下面找到我的文件转储,以及我收到的错误信息 在\AppBundle\Services中,我创建了包含以下内容的文件my_isbookchosencheck.php: <?php namespace AppB
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'));
}
}
我的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").
试一试
在您的services.yml中,并检查您是否使用了正确的名称空间
你的课很好,应该可以用,但是我可以建议你使用吗
symfony2会话服务您可以将其作为构造函数参数传递,而不是自己创建会话对象:
<?php
// namespace edited
namespace AppBundle\Services;
use Symfony\Component\HttpFoundation\Session\Session;
class my_isbookchosencheck
{
public function __construct(Session $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'));
}
}
另外,请查看他的问题:
试试看
在您的services.yml中,并检查您是否使用了正确的名称空间
你的课很好,应该可以用,但是我可以建议你使用吗
symfony2会话服务您可以将其作为构造函数参数传递,而不是自己创建会话对象:
<?php
// namespace edited
namespace AppBundle\Services;
use Symfony\Component\HttpFoundation\Session\Session;
class my_isbookchosencheck
{
public function __construct(Session $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'));
}
}
另外,请查看他的问题:
试试看
在您的services.yml中,并检查您是否使用了正确的名称空间
你的课很好,应该可以用,但是我可以建议你使用吗
symfony2会话服务您可以将其作为构造函数参数传递,而不是自己创建会话对象:
<?php
// namespace edited
namespace AppBundle\Services;
use Symfony\Component\HttpFoundation\Session\Session;
class my_isbookchosencheck
{
public function __construct(Session $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'));
}
}
另外,请查看他的问题:
试试看
在您的services.yml中,并检查您是否使用了正确的名称空间
你的课很好,应该可以用,但是我可以建议你使用吗
symfony2会话服务您可以将其作为构造函数参数传递,而不是自己创建会话对象:
<?php
// namespace edited
namespace AppBundle\Services;
use Symfony\Component\HttpFoundation\Session\Session;
class my_isbookchosencheck
{
public function __construct(Session $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'));
}
}
另外,请查看他的问题:
您犯的错误很少,我将简要解释一下,我将给您举一个您想要创建的服务的示例
- 您在
中创建了服务,但您的命名空间注册方式不同-AppBundle\Services
命名空间AppBundle\Services\my_isbookchosencheck代码>。它应该是
名称空间AppBundle\Services代码>。我还建议您在创建目录时使用单数名称-在这种情况下,
比Service
更好Services
- 您直接使用
\u构造函数
应用一些逻辑并返回结果。更好的方法是创建一个自定义方法,必要时可以访问该方法
- 您正在创建会话的新实例,这意味着您将无法访问先前在会话中添加和存储的任何内容。这里的正确方法是注入
,它保存当前的RequestStack
,并从那里获取会话请求
- 我相信你的服务也注册错了。在您的
文件中,它应该位于services.yml
选项下。这就是为什么会出现粘贴的错误services:
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()
}
}
现在,在控制器中,您可以像这样简单地使用它:
$this->get('book_service')->isBookChoosen()
services:
my_service:
class: Acme\DemoBundle\Service\MyService
arguments:
- @session
这是一个简短的示例,但我希望您能理解。您犯了一些错误,我将简要解释一下,我将为您提供一个您想要创建的服务的示例
- 您在
中创建了服务,但您的命名空间注册方式不同-AppBundle\Services
命名空间AppBundle\Services\my_isbookchosencheck代码>。它应该是
名称空间AppBundle\Services代码>。我还建议您在创建目录时使用单数名称-在这种情况下,
比Service
更好Services
- 您直接使用
\u构造函数
应用一些逻辑并返回结果。更好的方法是创建一个自定义方法,必要时可以访问该方法
- 您正在创建会话的新实例,这意味着您将无法访问先前在会话中添加和存储的任何内容。这里的正确方法是注入
,它保存当前的RequestStack
,并从那里获取会话请求
- 我相信你的服务也注册错了。在您的
文件中,它应该位于services.yml
选项下。这就是为什么会出现粘贴的错误services:
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()
}
}
现在,在控制器中,您可以像这样简单地使用它:
$this->get('book_service')->isBookChoosen()
services:
my_service:
class: Acme\DemoBundle\Service\MyService
arguments:
- @session
这是一个简短的示例,但我希望您能理解。您犯了一些错误,我将简要解释一下,我将为您提供一个您想要创建的服务的示例
- 您在
中创建了服务,但您的命名空间注册方式不同-AppBundle\Services
命名空间AppBundle\Services\my_isbookchosencheck代码>。它应该是
名称空间AppBundle\Services代码>。我还建议您在创建目录时使用单数名称-在这种情况下,
比Service
更好Services
- 您直接使用
\u构造函数
应用一些逻辑并返回结果。更好的方法是创建一个自定义方法,必要时可以访问该方法
- 您正在创建
会话的新实例,这意味着您将无法访问以前添加和存储的任何内容