0

I am using Zend Framework v 1.10

I have created a custom function in the bootstrap file:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function init(){   }

    public function helloworld(){ echo 'hello';}
}
?>

How do I call the helloworld() function from an Action within the Index Controller?

Any help will be appreciated.

Thanks

3 Answers 3

5
$this->getFrontController()->getParam('bootstrap')->helloworld();

should work. But I can't think of any reason why you would want to do this - the bootstrap is for initialising application resources, its job is done long before controllers get involved. Perhaps whatever you are doing in the method should be a resource or in a controller plugin?

Sign up to request clarification or add additional context in comments.

4 Comments

Then what is the difference between the bootstrap file and the index.php file that sits under the public folder which I found under the Quickstart code sample on the zend official site?
Both are part of the initialisation process. The index.php calls the bootstrap class.
I agree with Tim. The index.php is implementing the Front Controller pattern (en.wikipedia.org/wiki/Front_Controller_pattern) in that it's the central point that handles all requests and looks after the dependent processes for the application to operate effectively - such as bootstrapping. The bootstrap however is more specific in that it takes care of the initialisation of resources for the application, such as routing, caching, navigation and pagination; amongst other things.
I'm not exactly sure what you're wanting to achieve here, but have you considered the preDispatch and postDispatch methods (framework.zend.com/manual/en/…) in controllers?
0

Anyone can call a function in the bootstrap class without making any object of the class. Bootstrap automatically calls custom functions which have the _init keyword as a prefix. Such as:

public function _initIndia() { 
     echo 'Proud to be an Indian'; 
}

Comments

-1

Any public functions you create in the bootstrap that begin with _init will automatically be called by the bootstrapping code. For instance:

public function _initHelloWorld() { echo 'hello'; }

2 Comments

The question asked about a function called helloworld, not _initHelloWorld so the OP seems to know that this is the case.
And by the way, it does not need to be public to be called Automatically. Protected Methods do get called Automatically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.