I am learning Zend framework myself. I am facing error on initial installation of zend framework.
For start I created a controller for books BooksController.php
<?php
class BooksController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
/* Initialize action controller here */
}
public function listAction()
{
echo "hi";
$bookTBL = new Model_Books();
}
}
I created Books model on file Books.php
<?php
class Model_Books extends Zend_Db_Table_Abstract {
protected $_name = 'books';
}
To load Model_Books model I created _initAutoload() function at Bootstrap.php
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload() {
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH
));
return $modelLoader;
}
}
I have also configured Database on application.ini
[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
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
database.adapter = mysql
database.params.host = localhost
database.params.username = root
database.params.password = 123456
database.params.dbname = zftutorial
[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
After these all coding when I tried to open http://localhost/zftutorial/public/books/list url in browser it is giving me Application error.
Please guide me where I have mistaken.
First of all make sure in which mode you are working if you dont know than simply specify it in your .htaccess like this:
Your file path: zftutorial/public/.htaccess
Add this line in it.
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
This will make sure that you are in development mode.
Remove this from your production mode for now:
database.adapter = mysql
database.params.host = localhost
database.params.username = root
database.params.password = 123456
database.params.dbname = zftutorial
and add this in your development mode:
[development : production]
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "123456"
resources.db.params.dbname = "zftutorial"
This might solve your issue.
Related
I have transfer zend framework from one server to another.
its working fine but Auth controller is not working. it got error page not found issue, while a user login. Even it login the user but redirect on page not found. I am not sure where I need to make changes. Is it server issue or configuration issue as it work fine in my other server.
mod_rewrite of apache is enabled, server is pointed to public with .htaccess
.htaccess file
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
application.ini
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
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = "default"
[staging : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[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
database.host = "localhost"
database.username = "xxx"
database.password = 'xxxxxx'
database.dbname = "xxx"
autoloadernamespaces[] = "Resources_"
autoloadernamespaces[] = "Plugins"
resources.frontController.plugins[] = "Plugins_ViewSetup"
index.php in public folder
defined('BASE_URL')
|| define('BASE_URL', 'http://11.11.11.11'); // Live
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
defined('CHARTS_FONT_PATH')
|| define('CHARTS_FONT_PATH', realpath(dirname(__FILE__) . '/../library/Charts/fonts'));
defined('PUBLIC_FOLDER')
|| define('PUBLIC_FOLDER', 'public');
defined('DATA_UPLOAD_PATH')
|| define('DATA_UPLOAD_PATH', realpath(dirname(__FILE__) . '/data'));
// 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';
/** Utilities */
require_once 'Utils.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
There is some issue in database view access issue. I just debug code and check the error. Fixed
Currently I am transferring zend 2 from an server to another. I got some path issue. While I am accessing file with index.php it work for once and than stop working. like url - 11.11.11.11/index.php/user/login. in this case it worked fine for once and after set user name and password. It stop working.
What I have done is pointed server on pulic folder
.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
application.ini
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
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = "default"
[staging : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[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
database.host = "localhost"
database.username = "xxx"
database.password = 'xxxxxx'
database.dbname = "xxx"
autoloadernamespaces[] = "Resources_"
autoloadernamespaces[] = "Plugins"
resources.frontController.plugins[] = "Plugins_ViewSetup"
index.php in public folder
defined('BASE_URL')
|| define('BASE_URL', 'http://11.11.11.11'); // Live
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
defined('CHARTS_FONT_PATH')
|| define('CHARTS_FONT_PATH', realpath(dirname(__FILE__) . '/../library/Charts/fonts'));
defined('PUBLIC_FOLDER')
|| define('PUBLIC_FOLDER', 'public');
defined('DATA_UPLOAD_PATH')
|| define('DATA_UPLOAD_PATH', realpath(dirname(__FILE__) . '/data'));
// 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';
/** Utilities */
require_once 'Utils.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
I have solved this issue by enable Apache mod_rewrite
I want to get use of Controller plugins in Zend Framework.
I've created a folder "plugins" in application folder and a file Test.php inside of it. It's content:
<?php
class Plugin_Multilanguage extends Zend_Controller_Plugin_Abstract{
public function preDispatch(){
echo 'The plugin is working';
}
}
in the Bootstrap file, I have added function:
protected function _initAutoload(){
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_Multilanguage());
}
And it doesn't work. I don't get even any kind of error or worning. Just a blank page. I have noticed that there plenty of similar topics, I've read all of them, but I'm still confused with that simple issue. I use Zend Framework 1.11. Please help.
I got following error:
Fatal error: Class 'Plugin_Multilanguage' not found in C:\zendSites\moduleDeveloper\application\Bootstrap.php on line 13
My application.ini file content:
[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules = ""
resources.frontController.params.displayExceptions = 0
resources.frontController.params.prefixDefaultModule = "1"
resources.router.routes.defaultmodule.type = Zend_Controller_Router_Route_Module
resources.router.routes.defaultmodule.abstract = On
resources.router.routes.defaultmodule.defaults.module = "system"
resources.router.routes.language.type = Zend_Controller_Router_Route
resources.router.routes.language.route = ":language"
resources.router.routes.language.reqs.language = "^(fr|en)$"
resources.router.routes.language.defaults.language = "en"
resources.router.routes.default.type = Zend_Controller_Router_Route_Chain
resources.router.routes.default.chain = "language, defaultmodule"
[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've tried to add one by one following resources:
pluginPaths.Plugins = APPLICATION_PATH "/plugins"
autoloaderNamespaces[] = "plugins"
resources.frontController.plugins[] = "Plugin_Multilanguage"
pluginPaths.Plugins = APPLICATION_PATH "/plugins"
autoloaderNamespaces[] = "plugins"
Only the error message is changing:
Fatal error: Class 'Plugin_Multilanguage' not found in C:\zend\library\Zend\Application\Resource\Frontcontroller.php on line 117
What am I doing wrong?
The chances are that your plugin path is not readable by the application:
APPLICATION_PATH "/plugins"
These kind of paths are not readable by default for class code in ZF.
Try moving your plugins to an Application library:
/library
/Application
/Plugin
Multilanguage.php
Update your code to reflect this:
<?php
class Application_Plugin_Multilanguage extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(){
echo 'The plugin is working';
}
}
And update the bootstrap:
protected function _initAutoload()
{
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Application_Plugin_Multilanguage());
}
I am fairly new to zend framework. I have been trying to write RESTful controller using Zend_Rest_Controller. I built one from a good tutorial
http://www.techchorus.net/create-restful-applications-using-zend-framework
It works perfectly. So I went ahead added it to the existing zend application. I just wanted one controller to be RESTful so did the necessary changes in the bootstrap.
protected function _initRestRoute()
{
$this->bootstrap('frontController');
$frontController = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($frontController, array() , array('default' => array('MyserviceController')));
$frontController->getRouter()->addRoute('rest', $restRoute);
}
the server throws up a 500 internal server error when i try to access the service using the url http://localhost/projectname/public/index.php/myservice which is supposed to invoke the index method from the MyserviceController. The application has the following folder structure
application/
configs/
application.ini
controllers/
IndexContoller
OneController
TwoController
Myservice
forms
layouts
models
modules/
app1module
app2module
app3module
views
Bootstrap.php
this is the application.ini for the project
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = "America/New_York"
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/"
includePaths.helpers = APPLICATION_PATH "/views/helpers"
;Module support
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.defaultModule = "default"
resources.modules[] = ""
resources.db.adapter = PDO_MYSQL
resources.db.params.host = ********
resources.db.params.username = *********
resources.db.params.password = *********
resources.db.params.dbname = *********
[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
and this the code for MyserviceController.php
<?php
class MyserviceController extends Zend_Rest_Controller
{
public function init()
{
parent::init();
$this->_helper->viewRenderer->setNoRender(true);
}
public function indexAction() {
$this->getResponse()->setBody('Hello World');
$this->getResponse()->setHttpResponseCode(200);
}
public function getAction() {
$this->getResponse()->setBody('Foo!');
$this->getResponse()->setHttpResponseCode(200);
}
public function postAction() {
$this->getResponse()->setBody('resource created');
$this->getResponse()->setHttpResponseCode(200);
}
public function putAction() {
$this->getResponse()->setBody('resource updated');
$this->getResponse()->setHttpResponseCode(200);
}
public function deleteAction() {
$this->getResponse()->setBody('resource deleted');
$this->getResponse()->setHttpResponseCode(200);
}
}
?>
In "localhost/projectname/public/index.php/myservice" why you using index.php, why not just /index/ ?
ZF's default URL structure is the "http://hostname/controller/action/parametes"
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.