Zend Framework module in command line - zend-framework

I have Zend Framework command line application. All models and stuff that I use in this application are now in default module (Application prefix) but I want to move them to cli module. When I moved my model to application/cli/model folder and renamed class names then autoloader can't find them. I have also admin module and it is working fine.
Here is my server.php file:
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(__DIR__ . '/../application'));
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
require_once 'Zend/Application.php';
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->getBootstrap()->bootstrap(array('date', 'config'));
and here is my application.ini:
[production]
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 = 0
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.modules[] = ""
What should I do to make modules work in command line mode?

I would guess you're missing the bootstrap file for your Cli module - it should live at application/modules/cli/Bootstrap.php. You might also need to ensure the module and front controller resources are initialized by adding them to your array, giving you:
$application->getBootstrap()->bootstrap(array('date', 'config', 'modules', 'frontController'));
If not we'll need more info, including the error you're getting, the class you are trying to use and where it is defined.

WHat you need is a cli entry point.
Normally you enter the app on index.php (or /) in your browser aka via http. This will normally build a response and a router. Because you are not comming via the web you have to tweak this a bit.
So create a file which you use as your entry point for example cli.php (copy it from public/index.php). And make some tweaks in it
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
// bootstrap and retrieve the frontController resource
$front = $application->getBootstrap()
->bootstrap('frontController')
->getResource('frontController');
//Which part of the app we want to use?
$module = 'default'; //or other module
$controller = '<your controller>';
$action = '<your action>';
//create the request
$request = new Zend_Controller_Request_Simple ($action, $controller, $module, $options);
// set front controller options to make everything operational from CLI
$front->setRequest($request)
->setResponse(new Zend_Controller_Response_Cli())
->setRouter(new Custom_Controller_Router_Cli())
->throwExceptions(true);
// lets bootstrap our application and enjoy!
$application->bootstrap()
->run();

Related

Zend Command Line can't find model in modules

Sorry for my English, but please help me. How can i find model in modules. I used Zend Framework 1.12 and i run php file in command line. Example code:
//file located in project/scripts/test.php
<?php
//ini_set("display_errors","1"); ini_set("error_reporting", E_ALL);
// Initialize the application path and autoloading
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', $argv[1]);
set_include_path(implode(PATH_SEPARATOR, array(
APPLICATION_PATH . '/../library',
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
// Initialize Zend_Application
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
//this model i can find
$sendQueueModel = new Application_Model_DbTable_SendQueue();
//but this i can't. Managers - this is module in project
$managersMOdel = new Managers_Model_Company();
That module is not loading thats why you cant access models. In normal way we add autoloader in Bootstap.php, here you should try the same code to load it. (after initializing Zend_Application)
$autoLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Managers',
'basePath' => APPLICATION_PATH . '/modules/managers'
));

Cannot find application.php

hello am trying to install zend framework again after formating my machine but i keep getting this error...i have already done one project from previous installation but this time installation has just failed..
>>>Warning: require_once(Zend/Application.php) [function.require-once]: to open stream:
No such file or directory in C:\xampp\htdocs\home\public\index.php on line 18
Fatal error: require_once() [function.require]: Failed opening required 'Zend/Application.php' (include_path='C:\xampp\htdocs\home\library;“.') in C:\xampp \htdocs\home\public\index.php on line 18
This is my index.php page
>>// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
//
The application.php is already inside the library/zend package...
This is my application.ini
[production]
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 = 0
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
I just dont understand why it cant get that file yet it is inside the Zend folder...
Everything looks just fine from my perspective
Pliz help...thanks in advance
Make sure that your ZF library folder is in you include path in php.ini.
On my machine (typical windows + xampp), the file can be found in c:\xampp\php\php.ini
Look for the line:
include_path = "."
and change it to include you ZF library path
include_path = ".;C:\path\to\Zend\Parent\Directory";
If your ZF libraries are located in C:\xampp\ZF\library\Zend, the include path should be "C:\xampp\ZF\library".

Getting My ZF Application Seeing My Subdomain Routes

I'm working on a project that I'm using Zend_Hostname_Router_Route for.
My application is fairly simple. I have a a module named dev which I want to route to the dev subdomain. I have read a few articles on here and other sites to try to get a idea of what the problem is.
I am using ZF 1.12 for my application. I'm using the Zend_Hostname_Router_Route in my appllication.ini file as listed below:
[production]
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.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.contentType = "text/html; charset=UTF-8"
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook.db"
[development:production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/..application/db/guestbook-dev.db"
resources.router.routes.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.route = "dev.michaeldrowan.com"
resources.router.routes.chains.dev.type = "Zend_Controller_Router_Route"
resources.router.routes.chains.dev.route = ":controller/:action/"
resources.router.routes.chains.dev.defaults.module = "dev"
resources.router.routes.chains.dev.defaults.controller = "index"
resources.router.routes.chains.dev.defaults.action = "index"
The application in the ZF quickstart tutorial. So I shouldn't have problems setting this up. I have tried two index.php. On in public_html and one in subdomain in folder below(that is where my host puts it in). The index in the subdomain points to the dev module..I also tried the way of doing it that is how the zf quickstart tutorial
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../michaeldrowan /application/'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
?>
and the index in the dev is:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
?>
I have tried to point to the module in both by changing the 'production' to 'development'. I even tried 'development : production'
I've also tried to point the application to the module config and tried setting the application to the module too.
I'm running out of things to try. I hope someone can help me.
If you are using Apache write this in your .htaccess
RewriteCond %{HTTP_HOST} ^([a-zA-Z\-\.]+)\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ index.php?someparam=%1 [QSA]
Then you can access subdomain name in your IndexController:
if ($this->getRequest()->getParam('someparam') == 'dev') {
// use routing
}

APPLICATION_PATH in *.ini configuration, how does it work?

An *.ini file has a constant: APPLICATION_PATH
When is APPLICATION_PATH set and how does it work?
See the below code for instance
; application/configs/application.ini
[production]
; PHP settings we want to initialize
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 = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
APPLICATION_PATH is a PHP constant used by ZendFramework to determine where you deployed/installed your project. It's usually defined in newproject/public/index.php i.e.
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH',
realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
Application.ini is not a php class / file, it is a configuration file which means it follows a different syntax.
To concatenate strings and constants together, you simple put them next to each other, you don't use the dot (.) operator. One thing to watch out for is that you have to use double quotes ("), otherwise the constant will not be evaluated.
For more information, you can look into the documentation of the parse_ini() function, which is the function that's used by ZendFramework to parse configuration files.
References:
http://php.net/manual/en/function.parse-ini-file.php
http://php.net/manual/en/function.constant.php
http://framework.zend.com/manual/en/zend.application.quick-start.html
You should find its definition in public_html/index.php
Zend Frameworks uses two important dynamic constants(APPLICATION_PATH, APPLICATION_ENV) for entire framework to work correctly in what ever location it is hosted.
it has to be initialised before there are used. so there are initialised in the index.php as every request will go through the index.php in zend framework.

Problems integrating Zend Framework and Doctrine

I've done this before on another machine and the setup is almost identical. However, its just not working on the new machine. When I try and get Doctrine to generate my models, its putting them in the wrong place. Its creating an extra "Model" directory both for the base classes as well as the regular classes. I'm using Doctrine 1.2.3 with ZF 1.11.0. Here is my ZF application.ini:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = "America/Denver"
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = ""
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.view[] =
autoloaderNamespaces[] = "Doctrine_"
autoloaderNamespaces[] = "App_"
doctrine.connection_string = "mysql://root#localhost/mydb"
;doctrine.cache = true
doctrine.data_fixtures_path = APPLICATION_PATH "/doctrine/data/fixtures"
doctrine.sql_path = APPLICATION_PATH "/doctrine/data/sql"
doctrine.migrations_path = APPLICATION_PATH "/doctrine/migrations"
doctrine.yaml_schema_path = APPLICATION_PATH "/doctrine/schema"
doctrine.models_path = APPLICATION_PATH "/models"
doctrine.generate_models_options.pearStyle = true
doctrine.generate_models_options.generateTableClasses = true
doctrine.generate_models_options.generateBaseClasses = true
doctrine.generate_models_options.baseClassPrefix = "Base_"
doctrine.generate_models_options.baseClassesDirectory =
doctrine.generate_models_options.classPrefixFiles = false
doctrine.generate_models_options.classPrefix = "Model_"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
Here is my Doctrine function from my ZF Bootstrap.php file:
protected function _initDoctrine()
{
$this->getApplication()->
getAutoloader()->
pushAutoloader(array('Doctrine_Core', 'autoload'));
$config = $this->getOption('doctrine');
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_');
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR);
$manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL);
$manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);
if (isset($config['cache']) && $config['cache'] == true) {
$cacheDriver = new Doctrine_Cache_Apc();
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
}
$connection = $manager->openConnection($config['connection_string'], 'doctrine');
$connection->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
$connection->setCharset('utf8');
return $connection;
}
And here is my doctrine.php command line script:
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path()
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->getBootstrap()->bootstrap('doctrine');
$config = $application->getOption('doctrine');
$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);
So, I'm expecting my models directory to end up like this:
models
Base
-Class1.php
-Class2.php
-Class1.php
-Class1Table.php
-Class2.php
-Class2Table.php
where the base class class names are formatted like Model_Base_Class1 and the regular class names are Model_Class1 (in the actual php files themselves). Instead, what I'm getting is:
models
Base
Model
-Class1.php
-Class2.php Model
-Class1.php
-Class1Table.php
See the extra Model directories? What is causing this? I've tried messing with the doctrine.generate_models_options.classPrefix option in my ini as well as the similar setting in my Bootstrap.php file - $manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_');
Please help me!
Well, I'm not so smart once again. :( All it was, was a misconfiguration of my schema.yaml file. I had put Model_User as the model name instead of just User! The fixtures file does require it to be Model_User though. Problem solved.