0

I'm trying to create own Model to do something (there will be calculations, moving in databases, and other non specifed work). But i got a problem, because I'm still getting and error that Class is not found.

Here is controller (module/Application/src/Application/Controller/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 (module/Application/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';
    }
}

and a model, Przesylki.php (module/Application/src/Application/Model/Przesylki.php):

namespace Main\Model;

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

if necessary, autoload_classmap.php (module/Application/src/autoload_classmap.php is just a

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',
        ],
    ],
];

Directories:

/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

And when I run /cennik i got a Fatal, that class is not found:

[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

Actual files and namespaces:

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

2 Answers 2

3

You have problems with your namespaces. Your module's name is Application so your Model namespace should be Application\Main\Model.

Your module structure is not the recommended one for ZF2. It should be something as:

/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
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for information. I tried to make directories as recommended, but right now all clasess can't be found, even index. I have changed code up there, can you check why why routing doesn't work? Error code now: [Tue Sep 06 11:23:59.214235 2016] [:error] [pid 4937] [client 195.8.99.234:59632] PHP Fatal error: Class 'Application\\Controller\\IndexController' not found in /var/www/Paczucha_pl/vendor/zendframework/zend-servicemanager/src/Factory/$
@KarolGasienica Do you update all your namespaces after updating your module structure? You have to check the namespaces of your whole application, according to your module structure.
Are you able to check what's wrong? I think I set it all right... github.com/Xantias/paczucha_public
@KarolGasienica I added the namespaces in my example. Please check you don't have typos in your directories names. Please have a look on ZF2 documentation, check your autoloader is OK, etc. framework.zend.com/manual/2.4/en/modules/… I can't check your whole application...
There is nothing in yet :) It's almost clean app (only view is changed, but this doesnt matter). I will look later if i didnt make any mistake.
|
1

Okay finally I found a solution. Correct structure for given namespaces is :

/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

There shouldnt be double Application folder. I think it's Zend 3, maybe it changed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.