2016-09-06 53 views
0

我試圖創建自己的模型來執行某些操作(將會有計算,在數據庫中移動以及其他未指定的工作)。但是我遇到了一個問題,因爲我仍然收到錯誤,找不到類。PHP Zend 2:致命錯誤 - 未找到類

這裏是控制器(模塊/應用/ SRC /應用/控制器/ IndexController.php):

namespace Application\Controller; 

use Application\Model\Przesylki; 
use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

class IndexController extends AbstractActionController 
{  

    public function indexAction() 
    { 
     $this->layout('layout/layout'); 
    } 

    public function cennikAction() { 
     $this->layout('layout/pusty'); 
     $logika_paczki = new Przesylki(); 
     echo $logika_paczki->return_word(); 
    } 
} 

Module.php(模塊/應用/ SRC/Module.php):

namespace Application; 

use Main\Model\Przesylki; 

class Module 
{ 
    const VERSION = '3.0.2dev'; 

    public function getAutoloaderConfig() 
    { 
     return array(
      'Zend\Loader\ClassMapAutoloader' => array(
       __DIR__ . '/autoload_classmap.php', 
      ), 
      'Zend\Loader\StandardAutoloader' => array(
       'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, 
       ), 
      ), 
     ); 
    } 

    public function getConfig() 
    { 
     return include __DIR__ . '/../config/module.config.php'; 
    } 
} 

和模型,Przesylki.php(模塊/應用/ SRC /應用/型號/ Przesylki.php):

namespace Main\Model; 

class Przesylki 
{ 
    public function return_word() { 
     return "word"; 
    } 
} 

如果必要的話, autoload_classmap.php(模塊/應用/ src目錄/ autoload_classmap.php只是

return array(); 

module.config.php:

namespace Application; 

use Zend\Router\Http\Literal; 
use Zend\Router\Http\Segment; 
use Zend\ServiceManager\Factory\InvokableFactory; 

return [ 
    'router' => [ 
     'routes' => [ 
      // Home 
      'home' => [ 
       'type' => Literal::class, 
       'options' => [ 
        'route' => '/', 
        'defaults' => [ 
         'controller' => Controller\IndexController::class, 
         'action'  => 'counter', 
        ], 
       ], 
      ], 
      'work' => [ 
       'type' => Literal::class, 
       'options' => [ 
        'route' => '/work', 
        'defaults' => [ 
         'controller' => Controller\IndexController::class, 
         'action'  => 'index', 
        ], 
       ], 
      ], 
      'cennik' => [ 
       'type' => Literal::class, 
       'options' => [ 
        'route' => '/cennik', 
        'defaults' => [ 
         'controller' => Controller\IndexController::class, 
         'action'  => 'cennik', 
        ], 
       ], 
      ], 
      // Panel 
      'panel' => [ 
       'type' => Segment::class, 
       'options' => [ 
        'route' => '/panel', 
        'defaults' => [ 
         'controller' => Controller\PanelController::class, 
         'action'  => 'index', 
        ], 
       ], 
      ], 
      'test' => [ 
       'type' => Segment::class, 
       'options' => [ 
        'route' => '/panel/test', 
        'defaults' => [ 
         'controller' => Controller\PanelController::class, 
         'action'  => 'test', 
        ], 
       ], 
      ], 
     ], 
    ], 
    'controllers' => [ 
     'factories' => [ 
      Controller\IndexController::class => InvokableFactory::class, 
      Controller\PanelController::class => InvokableFactory::class, 
     ], 
    ], 
    'view_manager' => [ 
     'display_not_found_reason' => true, 
     'display_exceptions'  => true, 
     'doctype'     => 'HTML5', 
     'not_found_template'  => 'error/404', 
     'exception_template'  => 'error/index', 
     'template_map' => [ 
      'layout/layout'   => __DIR__ . '/../view/layout/layout.phtml', 
      'layout/panel'   => __DIR__ . '/../view/layout/panel_layout.phtml', 
      'layout/counter'   => __DIR__ . '/../view/layout/counter.phtml', 
      'layout/pusty'   => __DIR__ . '/../view/layout/pusty.phtml', 
      'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 
      'error/404'    => __DIR__ . '/../view/error/404.phtml', 
      'error/index'    => __DIR__ . '/../view/error/index.phtml', 
     ], 
     'template_path_stack' => [ 
      __DIR__ . '/../view', 
     ], 
    ], 
]; 

目錄:

/config 
    /autoload 
/data 
    /cache 
/module 
    /Application 
     /config 
      module.config.php 
     /src 
      /Application 
       /Controller 
        IndexController.php 
        PanelController.php 
       /Model 
        Przesylki.php 
      autoload_classmap.php 
      Module.php 
     /test 
     /view 
/public 

當我運行/我有一個致命的,該類沒有被發現:

[Tue Sep 06 08:58:20.446371 2016] [:error] [pid 4934] [client 195.8.99.234:1129] PHP Fatal error: Class 'Main\\Model\\Przesylki' not found in /var/www/Paczucha_pl/module/Application/src/Main/IndexController.php on line 27 

實際的文件和命名空間:

Przesylki.php - Application\Model 
IndexController - Application\Controller 
PanelController - Application\Controller 
module.config.php - Application 
Module.php - Application 

回答

1

好吧終於我找到了解決方案。給定名稱空間的正確結構是:

/module 
    /Application 
     /config 
      module.config.php 
     /src 
      /Controller 
       IndexController.php // => Namespace : Application\Controller 
       PanelController.php // => Namespace : Application\Controller 
      /Model 
       Przesylki.php   // => Namespace : Application\Model 
     /view 
     /index 
      /index 
     /panel 
      /panel 

不應該有雙重Application文件夾。我認爲它是Zend 3,也許它改變了。

2

你必須與你的命名空間的問題。你的模塊名稱是Application,所以你的模型命名空間應該是Application\Main\Model

您的模塊結構不適用於ZF2。它應該是這樣的:

/module 
    /Application 
     /config 
      module.config.ph 
     /src 
      /Application 
       /Controller 
        IndexController.php // => Namespace : Application\Controller 
        PanelController.php // => Namespace : Application\Controller 
       /Model 
        Przesylki.php   // => Namespace : Application\Model 
     /view 
      /index 
       /index 
      /panel 
       /panel 
+0

感謝您的信息。我試圖按照建議製作目錄,但現在無法找到所有分類,甚至找不到索引。我在那裏更改了代碼,你能檢查爲什麼路由不起作用嗎? 錯誤代碼現在: [Tue Sep 06 11:23:59.214235 2016] [:error] [pid 4937] [client 195.8.99.234:59632] PHP致命錯誤:Class'Application \\ Controller \\ IndexController'找不到在/ var/www/Paczucha_pl/vendor/zendframework/zend-servicemanager/src/Factory/$ –

+0

@KarolGasienica更新模塊結構後,是否更新所有命名空間?根據您的模塊結構,您必須檢查整個應用程序的命名空間。 –

+0

你能檢查有什麼問題嗎?我想我把它設置好了... https://github.com/Xantias/paczucha_public –

相關問題