Need to call some function from another controller (not IndexController), for example:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$someFunction = new CustomController();
$someFunction->someFunc();
}
}
But this throws an error :
Fatal error: Class 'CustomController' not found in C:\xampp\htdocs\demo.ru\application\controllers\IndexController.php on line 13
If YourController::memberFunction() does something that is needed across multiple controllers, put that code in a action helper or library class,
so that both controllers can access the shared functionality without having to depend on each other.
I would suggest you to follow DRY and move those functions to common library place.
to use with Namespace see
Zend Framework calling another Controller Action
hope this will sure help you.
Related
Here I want to use session in my all pages any one can help me on this topic. some says create a custom controller in library, and/or use helper/hook, I don't know what will work.
If any one have a little example code and little desc. then please share here.
How can I use session in all pages.?
From the CodeIgniter documentation
Sessions will typically run globally with each page load, so the
Session class should either be initialized in your controller
constructors, or it can be auto-loaded by the system. For the most
part the session class will run unattended in the background, so
simply initializing the class will cause it to read, create, and
update sessions when necessary.
To initialize the Session class manually in your controller
constructor, use the $this->load->library() method:
$this->load->library('session');
So, in order to initialize the Session library in ALL your controllers, the best idea is to have a custom "general controller"
<?php
class MyController extends CI_Controller {
public function __construct()
{
$this->load->library('session');
}
}
and then have all your contollers extend not the default CI_Controller, but your "MyContoller", like in
<?php
class Blog extends MyController {
public function index()
{
echo 'Hello World!';
}
}
I hope this is what you need.
I'm using Castle Windsor, which generally rocks, however I want it to call a method on my component when it is created and seem to have hit a limitation with OnCreate. For exaxmple:
interface IService1
{
void op1();
}
interface IService2
{
void op2();
}
class MyComponent : IService1, IService2
{
public void Init() // not part of either service
{
}
public void op1()
{
}
public void op2()
{
}
}
// I want to call the component's Init() method when it's created, which isn't part of the service contract
container.Register(Component.For<IService1, IService2>().ImplementedBy<MyComponent>().OnCreate(s => s.Init()));
// I could call something from IService1
container.Register(Component.For<IService1, IService2>().ImplementedBy<MyComponent>().OnCreate(s => s.op1()));
// But I can't call anything from any of the other services
container.Register(Component.For<IService1, IService2>().ImplementedBy<MyComponent>().OnCreate(s => s.op2()));
The first registration won't compile, complaining that it "cannot resolve symbol Init" because the instance passed to the delegate is of type IService1. OnCreate seems a bit limited to me, as in the third case when there are multiple services exposed it only allows you to bind to the first one you declare. I'd have to swap IService1 and IService2 around in order to call op2, but that's just moving the problem around.
Why isn't the type passed in the delegate that of the component being registered? Then I'd be free to call whatever method I like. Is there a way around this? Assume I can't put the Init() code in the component's constructor.
Don't be constrained by the strongly typed nature of C#
Yes, the way the API is constructed it's based off of the first service of the component but you can always cast it down to its actual type (or a secondary service)
.OnCreate(s => ((MyComponent)s).Init())
Alternatively, implement Castle.Core.IInitializable or System.ComponentModel.ISupportInitialize (if you don't want your components to reference Windsor) and then you won't need .OnCreate() at all.
For future reference, here's the relevant documentation.
I'm new to Robotlegs, and somewhat struggling with the first steps.
To learn the ropes I followed the first part of Joel Hooks' introduction and am now trying to create the same in RL2.
My current questions/problems are:
Which method has replaced the startup() in the Context
In the Context I can no longer just use "mediatorMap"; Do I need to create a MediatorMap Instance for this?
In the few RL2 example out there, many devs use the IConfig to configure their main Context; is this required, a good convention or optional.
..and in which way, is the Context 'configured' through this?
I spent the best part of the day looking for solutions. I apologize if I missed the obvious.
Thanks in advance for any incite.
part of the main class (mxml)
xmlns:context="contexts.*"
<fx:Declarations>
<rl:ContextBuilder>
<context:HelloWorldContext/>
</rl:ContextBuilder>
</fx:Declarations>
and the (non-compling) context .as
public class HelloWorldContext extends Context
{
public function HelloWorldContext()
{
this.install(MVCSBundle);
this.configure(HelloWorldConfig);
mediatorMap.map(ButtonView).toMediator(ButtonMediator); // Error: mediatorMap not defined
}
}
Which method has replaced the startup() in the Context In the Context
use context.install to install extension that you want to use.
most common one is MVCSBundle.
use context.configure with (new ContextView) argument will start your context initialization.
I can no longer just use "mediatorMap"; Do I need to create a
MediatorMap Instance for this?
you can inject IMediatorMap anywhere you need it, like in config
[Inject]
public var injector:IInjector;
[Inject]
public var mediatorMap:IMediatorMap;
[Inject]
public var commandMap:ISignalCommandMap;
In the few RL2 example out there, many
devs use the IConfig to configure their main Context; is this
required, a good convention or optional. ..and in which way, is the
Context 'configured' through this?
you can create your own config. Usually, in there you will map your commands, mediators and injections. You create one [PostConstruct] method which will call all theese stuff:
If you implement IConfig inside config, you dont need [PostConstruct] tag, because robotlegs will call configure function automatically once the dependencies have been injected.
[PostConstruct]
public function init():void {
context.logLevel = LogLevel.DEBUG;
// injector.fallbackProvider = new DefaultFallbackProvider();
mapSignalCommands();
mapMediators();
mapInjection();
context.afterInitializing(afterInit);
}
in your case you will have
_context:IContext;
public function MainApp()
{
_context = new Context();
_context.install(MVCSBundle);
_context.configure(HelloWorldConfig,new ContextView(this));
}
mediator thing goes to config on mapMediators();
In an attempt to try and narrow my query down, I'm still very new to Codeigniter framework...
I want to define global variables (eg. in autoloaded helpers) and use global mysql queries throughout my site - but I don't understand how to do the latter (global mysql queries).
I understand the concept of defining single variables in a helper... and I understand the concept of creating a single mysql query in a model, loading it in a controller and using it in a view file (with a foreach loop).
How (and where) do I create a mysql query that can be autoloaded (or whatever) and used anywhere on my site - without the need to load it in every controller?
this might help.. models would probably be your best bet. below i have given some insite on how to use controlelrs/models/libraries in general, i would put my mysql code into the appropriate model file. and call it via any controller
// Library/profiles.php
class My_library
{
protected $CI;
public function __construct()
{
$this->CI =& get_instance(); // Existing Code Igniter Instance
}
public function my_lib_method()
{
// Your Code Here
// can communicate back with CI by using $this->CI
// $this->CI->load->view(....);
// $this->CI->load->model(...);
// ETC
}
}
// models/my_model.php
class My_model extends CI_Model{
public function my_mdl_method(){
// Your Code Here
}
}
// controllers/my_controller.php
class My_controller extends CI_Controller
{
public function my_ctrl_method(){
$this->load->library('my_library');
$this->load->model('my_model');
// calling a library method
$this->my_library->my_lib_method();
// calling a model method
$this->my_model->my_mdl_method();
}
}
I am new to Zend framework and I have a problem.
I created a controller abstract class which implements the functions like:
protected function AddError($message) {
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('Errors');
$flashMessenger->addMessage($message);
$this->view->Errors = $flashMessenger->getMessages();
}
protected function activateErrors()
{
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('Errors');
$this->view->Errors = $flashMessenger->getMessages();
}
So for each controller I am able to use
$this->AddError($error);
And then I render $error in layout.
So I want not to deal with flashMesenger in every controller.
but I have to execute the activateErrors when each action is executed.
for example
I have an controller test
class TestController extends MyController {
public function indexAction() {
$this->AddError("Error 1");
$this->AddError("Error 2");
$this->activateErrors();
}
public function index1Action() {
$this->AddError("Esdsd 1");
$this->AddError("sddsd 2");
$this->activateErrors();
}
}
Is there a way that I could execute this activateErrors in each action for every controller at the end of action without duplicating the code.
I mean I do not want to include this code at every action. Maybe there is a way to include it in my abstract class MyController.
Anybody any Idea?
thanks
What about using a postDispatch hook, in your parent MyController ?
Quoting that page :
Zend_Controller_Action specifies two
methods that may be called to bookend
a requested action, preDispatch() and
postDispatch(). These can be useful in
a variety of ways: verifying
authentication and ACL's prior to
running an action (by calling
_forward() in preDispatch(), the action will be skipped), for instance,
or placing generated content in a
sitewide template (postDispatch()).
Maybe this might do the trick ?
I actually contributed an enhancement to FlashMessenger which provides a lot of the functionality you're looking for.