Module autoloader problem in zf - php

i have 3 moulde like blow ,
application
|
modules
|
default
|---models
|--views
|--forms
|--controller-
|-indexController
|-errorController
admin
|---models-
|-user.php
|--views
|--forms
|--controller--
|-userController
cars
|---models
|--views
|--forms
|--controller
bootstrap.php
and in my appliction.ini
i have this config
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules = ""
and in my bootsrap file i have this autoloader
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH ));
so i can not access to my models class in userController and other controllers
i have this code in my userController
$userModel = new admin_Model_Users();
but i got error can not find this class
and this class is in user.php
class admin_Model_Users extends Zend_Db_Table_Abstract
{
public function getListUser() {
}
}
someone may help where is my wrong and how could fix this problem?

Get rid of
$modelLoader = new
Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH ));
and add
Bootstrap.php in you module:
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
also name your class Admin_Model_Users and use it accordigly where appropriorate.
Use capital "U" in the filename, capital "A" in the class name. I mean names should be consistent and in accordance to ZF guidelines.
Also in ini file:
change entry to
resources.modules[] =

Try the following:
class admin_Model_Users extends Zend_Db_Table_Abstract
Should become
class Models_UsersAdmin extends Zend_Db_Table_Abstract
$userModel = new admin_Model_Users();
Should become
$userModel = new Models_UsersAdmin();
Update
in you're application.ini file where you register you're namespaces add the following line:
autoloaderNamespaces[] = "Models" ( or register the namespace "Models" at bootstrap )

;define modules after controller in application.ini file
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

Related

Get configuration Zend Framework 2

I have a project with Zend Framework and i trying to rewrite this peoject on Zend Framework 2. In old project i have some environment dependent settings in application.ini
[Prod]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
theme = Test
copyright = TestText
resources.db.adapter = pdo_mysql
resources.db.params.host = 127.0.0.1
resources.db.params.username = root
resources.db.params.password = adm$123
resources.db.params.dbname = test;
I controller some value from aplication.ini has retrived.
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
$aConfig = $bootstrap->getOptions();
$this->view->assign('theme', $aConfig['theme']);
$this->view->assign('copyright', $aConfig['copyright']);
I download skeleton-application with Zend Framework 2 , add new module. But how I can do similar in my new project? Where and how I should describe settings from old project? How I can retrieve it?
You can easily use the AbstractOptions class for your ZF2 module. Let 's assume your ZF2 module is called application. So it 's stored in /module/Application/ folder.
First you need the ModuleOptions class under /module/Application/src/Options/. In this class you can write down all your settings you need for your module. For example reasons I only write the copyright member in the class.
declare('strict_types=1');
namespace Application\Options;
use Zend\StdLib\AbstractOptions;
class ModuleOptions extends AbstractOptions
{
protected $copyright = 'my copyright';
public function getCopyright() : string
{
return $this->copyright;
}
public function setCopyright(string $copyright) : ModuleOptions
{
$this->copyright = $copyright;
return $this;
}
}
Further you need a factory for your module options class. This factory could look like the following example.
declare('strict_types=1');
namespace Application\options\Factory;
use Application\Options\ModuleOptions;
use Interop\Container\ContainerInterface;
class ModuleOptionsFactory
{
public function __invoke(ContainerInterface $container) : ModuleOptions
{
$config = $container->get('config');
$options = new ModuleOptions(isset($config['my_settings']) ? $config['my_settings'] : []);
return $options;
}
}
Basicly that 's all you need. Just wrap it up in your module.config.php like the following example.
...
'service_manager' => [
'factories' => [
ModuleOptions::class => ModuleOptionsFactory::class,
]
],
'my_settings' = [
'copyright' => 'another copyright text',
]
The ModuleOptions class takes the my_settings array from your module.config.php and makes it accessable wherever a service locator is.
Example for use in a controller
For example you could use the ModuleOptions class in a controller factory like in the following example.
class IndexControllerFactory
{
public function __invoke(ContainerInterface $container)
{
$container = $container->getServiceLocator();
$options = $container->get(ModuleOptions::class);
return new IndexController($options);
}
}
Your IndexController class looks something like this. In this example we avoid calling the service locator in the controller itself because this is a bad practice. We just pass the options as a argument in the constructor.
class IndexController extends AbstractActionController
{
protected $options;
public function __construct(ModuleOptions $options)
{
$this->options = $options;
}
public function indexAction()
{
return [
'copyright' => $this->options->getCopyright(),
];
}
Enjoy! ;)

How to check database connectivity in Zend Framework 1.12.11

I am new to Zend Framework and I have installed it on my local server.
Below is my application.ini:
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
resources.db.adapter = "pdo_mysql"
resources.db.params.dbname = "zendy"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
I have created a database named zendy and created a table named country with two rows.
I have created country.php in my models folder and below is my code for it:
class Application_Model_country extends Zend_Db_Table_Abstract
{
protected $_name = 'country';
protected $_primary = 'id';
}
Below mentioned code is my indexController.php:
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$c = new Application_Model_country();
$this->view->country=$c->fetchAll();
}
}
But it throw an error in browser.
An error occurred
Application error
I have tried to debug from ErrorController.php it display error like:
protected 'message' => string 'Primary key column(s) (id) are not columns in this table ()' (length=59)
But while I have created the country-table in my database I've already applied a primary key.
Can anyone please help me out why this error happens? Is my ZF-project connected to the database?
Or do i miss anything?

Zend Framework Bootstrap.php, Database Model, and Form

So I am working on a sample database project.
I have a LoginController.php, a Faculty database, and a login page (phtml).
I get the error Fatal error: Class 'Faculty_DB' not found in /usr/local/zend/apache2/htdocs/InternProject1/application/controllers/LoginController.php on line 25
In the LoginController.php I have the following (plus some more):
public function indexAction()
{
$login = new Form_Login();
//$this->view->login = $login;
$request = $this->getRequest();
if($request->isPost())
{
$data = $request->getPost();
//$this->isValid(
if($this->getRequest()->getPost())
{
$username = $request->getParam('username');
$password = $request->getParam('password');
// echo " What you entered is: $username and $password";
//line 24
$faculty = new Faculty_DB();
//then conditions for validation.
This references
class Faculty_DB extends Zend_Db_Table_Abstract
which is located in application/models/ directory
I have the following in Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload()
{
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace(array('App_'));
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' =>
array('form'=>
array('path' => 'forms/',
'namespace' => 'Form_'
),
),
));
return $autoLoader;
}
}
Any clue on how to fix this?
I tried the following:
protected function _initResourceAutoloader()
{
$autoloader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => 'Application',
));
$autoloader->addResourceType( 'model', 'models', 'Model');
return $autoloader;
}
but when I do that, it tells me it can't find my Login_Form;
Given that your application namespace is Application, try:
Put your faculty DB class in application/models/Faculty.php
Name it Application_Model_Faculty
Extend it from Application_Model_DbTable_Faculty
Put the DbTable class in application/models/DbTable/Faculty.php
Since you are using Zend_Application, it will take care of setting up the autoloader and special prefixes like Form_, Model_, Plugin_ etc for you, so you can safely remove the _initAutoload and _initResourceAutoloader from your Bootstrap.

Zend Framework - Zend_Loader_PluginLoader

I'm just really starting with the Zend Framework, and currently I'm having a problem with the Zend_Loader_PluginLoader.
I managed to get a module specific plugin working easily enough using the following code:
class Api_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initPlugins()
{
$loader = new Zend_Loader_PluginLoader(array(
'Api_Plugin' => 'application/modules/api/plugins',
));
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Api_Plugin_ErrorControllerSelectorPlugin());
}
}
Edit: The class file is located at application/modules/api/plugins/ErrorControllerSelectorPlugin.php
I then tried to adapt this to get a plugin loaded for the whole application using:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAppAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'App',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
protected function _initPlugins()
{
$loader = new Zend_Loader_PluginLoader(array(
'My_Plugin' => 'application/plugins',
));
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new My_Plugin_ModuleConfigLoaderPlugin());
}
}
But I'm getting errors:
Fatal error: Class 'My_Plugin_ModuleConfigLoaderPlugin' not found in /var/www/localhost/application/Bootstrap.php on line 22
Edit: The class file is located at application/plugins/ModuleConfigLoaderPlugin.php
So - since the files are where I would expect them to be as far as the prefix/path pairs sent to Zend_Loader_PluginLoader() and the code in both cases are the same, what's the difference?
How do I get it to recognise my application-level plugins?
If you want the app-level plugin to reside within the namespace My_, you either need to put the My folder out in the library folder or declare the app-level namespace to be My_.
Assuming that you already have other stuff within your top-level app that uses the App_ namespace, then the easiest thing would be the former: move your My folder out into the library.
So, the plugin would reside in:
library/My/Plugins/ModuleConfigLoaderPlugin.php.
Then make sure that your configs/application.ini registers the My_ namespace:
autoloaderNamespaces[] = "My_"
Then the app-level Bootstrap could contain something like:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAppAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'App',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
protected function _initPlugins()
{
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new My_Plugin_ModuleConfigLoaderPlugin());
}
}
Alternatively, since your plugin does not sem to require any params, you could instantiate it via configs/application.ini using:
resources.frontcontroller.plugins[] = "My_Plugin_ModuleConfigLoaderPlugin"

Zend framework: Forms in modules

What I want to do:
Create a number of modules with the forms folder inside them.
What I did:
Create a bootstrapper inside the module and added an _initAutoload function with the specific module name as namespace.
For instance, an admin module with the following bootstrapper:
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Admin_',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
}
My question is:
Is this the correct way of doing what I want? - I tried it without having the admin bootstrapper, but it couldn't find my form, until I added the bootstrapper.
Cheers
Chris
The autoloader is automatically set up for each module bootstrap. You don't need to configure it manually.
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {}
is all you need.
Then put your forms in /application/modules/admin/forms/.
Admin_Form_Myform extends Zend_Form {...}
For your custom resources, customize resourceAutoloader:
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
public function _initAuloload()
{
$resourceLoader = $this->_resourceAuloloader;
// var_dump($resourceLoader);
}
}
Remember to add also in your apllication.ini
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules = ""

Categories