Can't load Action Helpers in Zend-framework application - zend-framework

I'm trying to create an Action Helper, but I'm havin't a hard time loading it and I'm getting this error:
Message: Action Helper by name Usersession not found
In my controllers action method, where i'm trying to call this helper, I have this:
Zend_Controller_Action_HelperBroker::addPath('/helpers/');
Zend_Controller_Action_HelperBroker::addPrefix('Helper');
$userSession = $this->_helper->getHelper('Usersession');
$this->view->session = $userSession->eendersWat();
I'd actually prefere to load my helpers from bootstrap.php, but wasn't able to figure that out neither.
My helpers are located in application/controller/helpers. My helper filename is Usersession.php and the class is called Helper_Usersession
Any ideas why this isn't working?

I use something like the following in Bootstrap:
protected function _initHelperPath()
{
Zend_Controller_Action_HelperBroker::addPath(
APPLICATION_PATH . '/controllers/helpers',
'Application_Controller_Action_Helper_');
}
The helper class is then named 'Application_Controller_Action_Helper_Usersession' and the file is located in application/controllers/helpers/Usersession.php
Of course, this presumes you are using Application_ as your application namespace. In your case, it appears that you are using an empty application namespace and none of my wordy Controller_Action_ infix, so your's would be something like:
protected function _initHelperPath()
{
Zend_Controller_Action_HelperBroker::addPath(
APPLICATION_PATH . '/controllers/helpers',
'Helper_');
}

Related

How to add your own library to Zend Framework

So i have been designig an application to run on the Zend Framework 1.11 And as any programmer would do when he sees repeated functionalities i wanted to go build a base class with said functionalities.
Now my plan is to build a library 'My' so i made a folder in the library directory in the application. So it looks like this
Project
Application
docs
library
My
public
test
So i created a BaseController class in the My folder and then decided to have the IndexController in my application extend the BaseController.
The Basecontroller looks like this :
class My_BaseController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->test = 'Hallo Wereld!';
}
}
And the IndexController looks like this :
class WMSController extends My_BaseController
{
public function indexAction()
{
parent::indexAction();
}
}
As adviced by a number of resources i tried adding the namespace for the library in the application.ini using the line
autoloadernamespaces.my = “My_”
But when i try to run this application i recieve the following error
Fatal error: Class 'My_BaseController' not found in
C:\wamp\www\ZendTest\application\controllers\IndexController.php
Am i missing something here? Or am i just being a muppet and should try a different approach?
Thanks in advance!
Your original approach will work for you in application.ini, you just had a couple of problems with your set up.
Your application.ini should have this line:-
autoloadernamespaces[] = "My_"
Also, you have to be careful with your class names, taking your base controller as an example, it should be in library/My/Controller/Base.php and should look like this:-
class My_Controller_Base extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->test = 'Hello World!';
}
}
You can then use it like this:-
class WMSController extends My_Controller_Base
{
public function indexAction()
{
parent::indexAction();
}
}
So, you had it almost right, but were missing just a couple of details. It is worth getting to know how autoloading works in Zend Framework and learning to use the class naming conventions
I don't know about .ini configuration, but I add customer libraries like this (index.php):
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance()->registerNamespace('My_');

Helper class cannot be found by autoloader in my Zend Framework application

I have the problem that in my ZF App the action helper cannot be loaded. The error message is:
Action Helper by name Sunshine not found
The layout of my ZF app uses modules where I have the following structure:
application
modules
weather
controllers
helpers
I have registered the helper in the modules Bootstrap which is located in
application -> modules -> weather -> Bootstrap.php
Here is the code
<?php
class Weather_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initActionHelperBrokers()
{
Zend_Controller_Action_HelperBroker::addPath('controllers/helpers', 'Weather_Controllers_Action_Helper_');
}
}
<?php
class Weather_Controller_Action_Helper_Sunshine extends Zend_Controller_Action_Helper_Abstract
{
public function getSunrise()
{
return "06:00";
}
}
<?php
class Weather_ForecastsController extends Zend_Controller_Action
{
protected function getForecasts($date)
{
$sunrise = $this->_helper->Sunshine->getSunrise();
// tbc
}
What is it, what I'm doing wrong here?
EDIT: As suggested I tried to add the helper in the bootstrap with the full path, but I got the same error.
the addPath method expects the entire path to the helper directory. Change it to something like:
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/modules/weather/controllers/helpers', 'Weather_Controllers_Action_Helper_');
Alternatively, you can also add the paths via your application.ini:
resources.frontController.actionhelperpaths.Weather_Controllers_Action_Helper = APPLICATION_PATH "/modules/weather/controllers/helpers"

User defined functions in zend

Where do i place the user defined functions in zend framework. These functions will used across the framework in many controls, views or models. Do i need to convert this to a utility class? Or i can just keep it as a set of functions and include it in index.php.
what is the best practice for this?
Typically you would put your functions into a class in the library for the auto loader. Use the naming conventions for ZF to make life easier.
adjust your application.ini to add a namespace.
Examples:
//application.ini
autoloaderNamespaces[] = "My_"
//this would equate to the folder My in the folder library
/application
/library
/My
//any class you built would be named My_Classname and be called in your app by Classname()
<?php
class My_Classname {
public function myFunction() {
}
}
//in your conrtoller for example you might call
public function indexAction() {
$class = new My_Classname();
$class->myFunction();
//or if you declared myFunction() static...
$class = My_Classname::myFunction();
}
Make it by following ZF directory structure:
Make Action Helpers for Controllers and View Helpers for Views :
In your library folder which is set in set_include_path:
create library/My/View/Helper/Common.php
Like below:
class My_View_Helper_Common extends Zend_View_Helper_Abstract
{
public function common()
{
return $this;
}
public function getCity($id)
{
$registry = Zend_Registry::getInstance();
$DB = $registry['DB'];
$result = $DB->fetchPairs("select * from firm_dtl");
return $result;
}
}
OR Call in View:
$this->common()->getCity($id);
Same process fro action helpers:
Make in library/My/Action/Helper/Common.php

action parameters routing not working in Zend framework routes.ini

I'm trying to set up a route in Zend Framework (version 1.11.11) in a routes.ini file, which would allow be to match the following url:
my.domain.com/shop/add/123
to the ShopController and addAction. However, for some reason the parameter (the number at the end) is not being recognized by my action. The PHP error I'm getting is
Warning: Missing argument 1 for ShopController::addAction(), called in...
I know I could set this up using PHP code in the bootstrap, but I want to understand how to do this type of setup in a .ini file and I'm having a hard time finding any resources that explain this. I should also point out that I'm using modules in my project. What I've come up with using various snippets found here and there online is the following:
application/config/routes.ini:
[routes]
routes.shop.route = "shop/add/:productid/*"
routes.shop.defaults.controller = shop
routes.shop.defaults.action = add
routes.shop.defaults.productid = 0
routes.shop.reqs.productid = \d+
Bootstrap.php:
...
protected function _initRoutes()
{
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'routes');
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addConfig( $config, 'routes' );
}
...
ShopController.php
<?php
class ShopController extends Egil_Controllers_BaseController
{
public function indexAction()
{
// action body
}
public function addAction($id)
{
echo "the id: ".$id;
}
}
Any suggestions as to why this is not working? I have a feeling I'm missing something fundamental about routing in Zend through .ini files.
Apparently I'm more rusty in Zend than I thought. A few minutes after posting I realized I'm trying to access the parameter the wrong way in my controller. It should not be a parameter to addAction, instead I should access it through the request object inside the function:
correct addAction in ShopController:
public function addAction()
{
$id = $this->_request->getParam('productid');
echo "the id: ".$id;
}
I also realized I can simplify my route setup quite a bit in this case:
[routes]
routes.shop.route = "shop/:action/:productid"
routes.shop.defaults.controller = shop
routes.shop.defaults.action = index

External Php class in Yii?

Hello I need to use evalmath.class.php within a Yii application. How can I include it from my controller?
Something like this:
public function actionFormulas() {
include('models/evalmath.class.php');
$m = new EvalMath;
...
}
But the above code does not work. How can I include an external class within a controller?
In your example, to use include/require you probably need to add some path info with dirname(__FILE__).'/../models/...' or similar, but to do it within the Yii framework, you would first create an alias (usually in your main config file) with setPathOfAlias :
Yii::setPathOfAlias('evalmath', $evalmath_path);
Then you can use Yii::import like so:
Yii::import('evalmath', true);
and proceed as you were:
$m = new EvalMath();
..etc...
class ServiceController extends Controller
{
public function actionIndex()
{
Yii::import('application.controllers.back.ConsolidateController'); // ConsolidateController is another controller in back controller folder
echo ConsolidateController::test(); // test is action in ConsolidateController