2015-06-27 61 views
0

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

回答

1

你犯的幾個錯誤,我將簡要解釋,我會給你一個你想要創建的服務的例子。

  • 您在AppBundle\Services創建了服務,但您的命名空間註冊不同 - namespace AppBundle\Services\my_isbookchosencheck;。它應該是namespace AppBundle\Services;。我還建議您在創建目錄時使用單數名稱 - 在這種情況下,Service會更好,而不是Services

  • 您直接使用__constructor來應用某些邏輯並返回結果。更好的方法是創建一個自定義方法,在必要時可以訪問它。

  • 您正在創建Session的新實例,這意味着您將無法訪問以前添加並存儲在會話中的任何內容。這裏的正確方法是注入RequestStack,其中包含當前的Request並從那裏獲得會話。

  • 我相信你也註冊了你的服務錯誤。在您的services.yml文件中,它應該在services:選項下。這就是爲什麼你得到了你粘貼的錯誤。

所以,讓我們看看你的服務應該如何。

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() 
    } 

} 

現在在你的控制器,你可以簡單地使用這樣的:

$this->get('book_service')->isBookChoosen() 

嗯,這是短暫的xample,但我希望你明白了。

1

嘗試

services:  
    my_isbookchosencheck: 
     class:   AppBundle\Services\my_isbookchosencheck 

在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,所以服務容器將注入會話對象:

services:  
    my_isbookchosencheck: 
     class:   AppBundle\Services\my_isbookchosencheck 
     arguments:  [@session] 

還檢查了他的問題上如此: How do you access a users session from a service in Symfony2?

1

服務只是普通的PHP類,沒有什麼特別的。但是您必須註冊才能被系統識別。這裏是你如何做到這一點的步驟,

創建一個普通的PHP類(如果需要你可以注入其他服務)

namespace Acme\DemoBundle\Service; 

class MyService 
{ 
    private $session; 

    public function _construct(SessionInterface $session /* here we're injecting the session service which implements the SessionInterface */) 
    { 
     $this->session = $session; 
    } 

    // other methods go here, which holds the business logic of this class 
} 

OK,我們創建了一個類,我們需要註冊它能夠通過服務容器使用它,在這裏你如何做到這一點:

最簡單的方法就是把它變成config.yml文件,像這樣:

services: 
    my_service: 
     class: Acme\DemoBundle\Service\MyService 
     arguments: 
      - @session 

,或者另一種方式,是創建一個文件(例如services.yml,可在config文件夾),然後將其導入config.yml文件中(該文件的內容是一樣的,第一種方式):

imports: 
    - { resource: services.yml } 

或者,你可以創建一個services.yml(的內容文件與第一種方式相同)文件夾內的Resources文件夾,在Extension類(在DependencyInjection文件夾下),load方法下指定它(這種方式需要一些特殊的目錄和文件結構,請參閱doc)

class AcmeDemoExtension extends Extension 
{ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources')); 
     $loader->load('services.yml'); 
    } 
} 

在您的案例,您沒有註冊您的服務,服務容器找不到它。以上述方式之一註冊。