Reputation: 2904
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
Upvotes: 0
Views: 2902
Reputation: 2904
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.
Upvotes: 1
Reputation: 4315
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
Upvotes: 3