2016-02-07 30 views
0

的Zend框架的工作,我創建了側模型文件夾我的模塊src文件夾內的一個模式,我想用這個模型作爲服務,我調用的模型類Module.php配置,但是當我運行該應用程序時,它顯示錯誤ServiceNotFoundException。
Zend\ServiceManager\ServiceManager::createFromInvokable: failed retrieving "authdbservice(alias: AuthDbService)" via invokable class "Authentication\Model\AuthDb"; class does not exist ZF2 ServiceNotFoundException的,如何解決我使用

這裏是我的module.php文件 類模塊實現AutoloaderProviderInterface,

ConfigProviderInterface, ServiceProviderInterface 
    { 
     /** 
     * 
     * {@inheritDoc} 
     * @see \Zend\ModuleManager\Feature\AutoloaderProviderInterface::getAutoloaderConfig() 
     */ 
     public function getAutoloaderConfig() 
     { 
      return array(
       'Zend\Loader\ClassMapAutoloader' => array(
        __DIR__ . '/autoload_classmap.php', 
       ), 
       'Zend\Loader\StandardAutoloader' => array(
        'namespaces' => array(
         __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, 
        ), 
       ), 
      ); 
     } 
     /** 
     * 
     * {@inheritDoc} 
     * @see \Zend\ModuleManager\Feature\ConfigProviderInterface::getConfig() 
     */ 
     public function getConfig() 
     { 
      return include __DIR__ . '/config/module.config.php'; 
     } 
     /** 
     * 
     * {@inheritDoc} 
     * @see \Zend\ModuleManager\Feature\ServiceProviderInterface::getServiceConfig() 
     */ 
     public function getServiceConfig() 
     { 
      return array(
       'invokables' => array(
        'AuthService' => 'Authentication\Service\AuthService', 
        'AuthDbService' => 'Authentication\DbService\AuthDb', 
       ), 
      ); 
     } 
    } 


這裏是我authDb類

namespace Authentication\DbService; 
use Doctrine\ORM\EntityManager; 
use Authentication\Entity\User; 
use Authentication\Form\RegisterForm; 
class AuthDb { 

    protected $em; 

    protected function getEntityManger() { 
     if (null === $this->em) { 
      $this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); 
     } 
     return $this->em; 
    } 

    public function submitNewUser() { 
     $form = new RegisterForm(); 
     $form->get('submit')->setValue('Register'); 
     $request = $this->getRequest(); 
     if ($request->isPost()) { 
      $user = new User(); 
      $form->setInputFilter($user->getInputFilter()); 
      $form->setData($request->getPost()); 
      if ($form->isValid()) { 
       $user->exchangeArray($form->getData()); 
       $user->__set('password', md5($user->__get('password'))); 
       $this->getEntityManager()->persist($user); 
       $this->getEntityManager()->flush(); 
       return $this->redirect()->toRoute('auth', array('action' => 'login')); 
      } else { 
       return array('form' => $form, 'messages' => $form->getMessages()); 
      } 
     } 
    } 

} 

我的目錄和文件結構

./Authentication 
./Authentication/autoload_classmap.php 
./Authentication/Module.php 
./Authentication/view 
./Authentication/view/authentication 
./Authentication/view/authentication/auth 
./Authentication/view/authentication/auth/register.phtml 
./Authentication/view/authentication/auth/login.phtml 
./Authentication/config 
./Authentication/config/module.config.php 
./Authentication/src 
./Authentication/src/Authentication 
./Authentication/src/Authentication/Service 
./Authentication/src/Authentication/Service/AuthService.php 
./Authentication/src/Authentication/Controller 
./Authentication/src/Authentication/Controller/AuthController.php 
./Authentication/src/Authentication/Model 
./Authentication/src/Authentication/Model/AuthDbService.php 
./Authentication/src/Authentication/Entity 
./Authentication/src/Authentication/Entity/User.php 
./Authentication/src/Authentication/Form 
./Authentication/src/Authentication/Form/LoginForm.php 
./Authentication/src/Authentication/Form/RegisterForm.php 

我認爲這是小姐的配置理解爲我在這種情況下 任何想法解決這一問題?

回答

0


我只是找到了解決辦法,我不得不承認,這是對我非常複雜的,但現在它更是一個稍微容易一些, 因爲我想我的數據庫模型,在我的控制器和其他服務的訪問服務我必須通過使用設計模式的大部分(在這種情況下爲工廠)進行一些小的更改。 所以我把模型文件夾裏面的文件AuthDbService.php,但它並沒有正確的名稱空間,所以我不得不改變命名空間

namespace Authentication\Model(這是coorect命名空間)

我也改了類名從AuthDbAuthDbService

然後我可以在我的其他控制器中使用它。 我所以現在改變了我的getServiceConfig功能

public function getServiceConfig() 
{ 
    return array(
     'invokables' => array(
      'AuthService' => 'Authentication\Service\AuthService', 
      'AuthDbService' => 'Authentication\Model\AuthDbService', 
     ) 
    ); 
} 

我可以用它在我的控制器內,BU這裏是東西! 我想使用其他服務(AuthService)內AuthDbService,這裏是新的問題。因爲我不能用我的AuthService裏面AuthDbService

爲什麼呢? AuthService是僅根據Zend公司的ServiceManager和這樣做訪問,我需要能夠訪問AuthDbService類裏面的ServiceManager,只要它是一個型號不是控制器服務,它不是從AbstractActionController延伸技術上我沒有的ServiceManager訪問注入AuthService

因此我必須先在ServiceModel(AuthDbService)中注入ServiceManager,爲此我必須在我的Module.php getServiceConfig函數中再次進行一些更改。

,但在此之前,我添加代碼的這些行我AuthDbService

protected $serviceLocator; 

    /** 
    * serviceLocator Setter 
    * @param ServiceLocatorInterface $serviceLocator 
    * @author Ehsan Aghaei 
    */ 
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) 
    { 
     $this->serviceLocator = $serviceLocator; 
    } 

    /** 
    * serviceLocator Getter 
    * @return \Authentication\Model\Service\unknown 
    * @author Ehsan Aghaei 
    */ 
    protected function getServiceLocator() 
    { 
     return $this->serviceLocator; 
    } 

我只是做了setter和getter注入這個類裏面的ServiceManger對象內。 在那之後,我做我的更改getServiceConfig,如下

public function getServiceConfig() 
    { 
     return array(
      'invokables' => array(
       'AuthService' => 'Authentication\Service\AuthService', 
      ), 
      'factories' => array(
       'AuthDbService' => function ($sm) { 
        $authService = new AuthDbService(); 
        $authService->setServiceLocator($sm); 
        return $authService; 
       } 
      ), 
     ); 
    } 

現在我有機會到服務管理和服務管理器,我可以訪問其他服務。 這就是我解決我的問題的方式。 我希望它能幫助像我這樣的人。 ;)