Is there a way to retrieve the module name from the controller? Like Application in default case.
The module name is available from request object via the method getModuleName(). So, within a controller:
$request = $this->getRequest();
$moduleName = $request->getModuleName();
Related
I am working on zend module based structure for my project. As Zend_Auth class' default session storage is Zend_Auth. I changed according to module being called. Say for admin I use auth namespace Admin_Auth and for default module i use namespace name Default_Auth.
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('Admin_Auth'));
I am doing this because, if I do Zend_Session::destroy() it will destroy complete session even for Default Module. and so using namespace and so at logout Zend_Session::namespaceUnset('Admin_Auth');
each time in different controller I have to use
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('Admin_Auth'));
just to point corresponding session data.
I am considering to move it in module's Bootstrap.php like
protected function _initAuth(){
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('Admin_Auth'));
return $auth;
}
First, is that proper way?
Second, If it is then how can I access the return value $auth of _initAuth() in each controller? Please
I have an application that uses the Mojolicious framework. I have a table in the database that has a list of error response and additional details associated with it. I have created corresponding Result and Resultset to work with the DB table. There is also a controller to get details about the Error by interacting with the Resultset.
My idea is to Call an action in this controller that would get the details of the error that is passed to it (by another controller) by querying the database, add-in runtime information about the environment that requested for the resource that resulted in the error, create a response and return to the controller that called it.
I am struggling with the call from one controller to another. How do I do it in Mojolicious? I can pass the controller object ($self) to accomplish this, but is there a better way to do it, so that I separate entirely my error handling response from the calling controller?
In Mojolicious, you would probably want to pass that object around with a helper without creating a Mojolicious::Controller out of it:
In your main class:
sub startup {
my $app = shift;
# ...
my $thing = Thing->new(foo => 42);
$app->helper(thing => sub {$thing});
}
In your controller:
sub cool_action {
my $c = shift;
# ...
my $foo = $c->thing->gimmeh_foo('bar');
# ...
}
However, if you would like to prepare something (e.g. databases) for some actions, maybe under is helpful for you:
To share code with multiple nested routes you can [...]
PS: This feature of Mojolicious was previously named Bridges. Answer updated accordingly.
I am new to Zend. The problem i am getting is that i am not able to access resources.db.* configurations from application.ini
The way i am using to access is
$application->getOptions()
. It does not shows the resource.db.* properties.
Can any one help me ?
Do this instead
$params = Zend_Db_Table::getDefaultAdapter()->getConfig(); //return associative array
To do it the way you asked about, the easiest way I've found is to put everything in the registry during bootstrap:
//bootstrap.php
public function _initConfig {
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
}
to use these configs elsewhere in your application:
$db = Zend_Registry::get('config')->resources->db;
although if you are just trying to access the adapter registered in the application.ini:
$db = Zend_Db_Table::getDefaultAdapter();
Is there anyway that I can get the request params directly from the model in zend framework? I need it for a constructor, so it doesn't actually need contact with the database.
In a controller it's easy to get the request... Something like $this->getRequest()->getParam('id'); but in a model that won't work obviously. I just need to get an instance of the controller, and then I can call the getRequest()->getParam('id') methods.
Yes there is, but you are doing it wrong.
For the purposes of answering the question this is how you do it....(but don't). You should pass the param to your model, rather than getting the param.
How to get the Request from Anywhere
> $fc = Zend_Controller_Front::getInstance();
> $fc->getRequest()->getParam('id');
But how you should be doing it (Assuming within controller context)
$model = new Model();
$model->getItemById( $this->getRequest()->getParam('id') );
I'm trying to capture a URL parameter in my bootstrap file but after several attempts I'm not able to do it.
I've tried this but it does not work:
protected function _initGetLang() {
$frontController = Zend_Controller_Front::getInstance();
$lang= $frontController->getParam('lang');
}
Is this the right way to do it?
Thks.
You won't be able to access the request params from the bootstrap because it hasn't yet gone through the dispatch/routing process. I think you'd be better served by using a Controller Plugin, performing actions based on the URL is what they do best. Or if you absolutely have to do it in the bootstrap, getRequestUri() or $_GET is available, or you could write a quick script to parse the url yourself.
Edit:
I've done some silly stuff like this in the past before I figured out how plugins work:
/**
* Grab the module name without a request instance
*
* #return string The module name
*/
public static function getModuleName()
{
$uri = ltrim($_SERVER["REQUEST_URI"], "/");
$module = substr($uri, 0, strpos($uri, "/"));
return $module;
}
This would at least give you a module name that you could switch on in the bootstrap. You should be able to do anything you need with the plugins done correctly though.