From one of my controller plugin, I want to pass a value to the controller. I have tried it using the Zend_Registry functionality. But it is not working.
In my plugin, I have tried setting registry key as follows.
Zend_Registry::set('msg','hello world!');
echo Zend_Registry::get('msg');
In my controller's action method, I tried to access it as follows
Zend_Registry::get('msg');
but nothing returns. In plugin it sets the value and outputting it. Am i missing anything? Or is this not a right way to do this? If so can someone help me?
plugin below
class COM_Application_Controller_Plugin_TestPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$currentController = Zend_Controller_Front::getInstance()
->getRequest()
->getControllerName();
$moduleName = $request->getModuleName();
Zend_Registry::set('msg','Hello Word!');
}
}
Related
I am making a project with Laravel 5.6 and at the moment I am making a sidebar with links to access the functionality of specified controller. f.e. if I am in posts blade, it will show PostsController methods for the sidebar.
The problem is that every controller has different amount of methods, and I wouldn't want to make a mess with 10 different static layouts for sidebars.
Is there a way to access controller methods thru functionality that returns all methods of the controller to a view?
Or am I thinking this wrong.. If someone knows a better solution for this i'm all ears. :)
I know I can install packages for functionality but I want to know before that is there any simple solution.
EDIT1:
get_class_methods($this) returns following value:
Returned Methods of a Controller
I can add a validator that checks if "index" or "create" is present. Guess my problem was solved, thank you all who answered.
EDIT2:
The code that dumps the returned methods.
public function index()
{
$events = Event::all();
dd($controller = get_class_methods($this));
return view('events.index', compact(['events', 'controller']));
}
You could use the ´get_cass_methods´ function to grab all the methods on the controller class
function index() {
$methods = get_class_methods($this);
return view('posts', compact('methods'));
}
if you want to filter out methods from the parent class
function index() {
$methods = array_diff(get_class_methods($this),get_class_methods(get_parent_class()));
return view('posts', compact('methods'));
}
I have taken over a project written in CodeIgniter, which I have never used before. I have added a new file to views/pages called features.php, and have read on the internet that to make it accessible, I need to create a function in the controller file that will render the page.
I have tried the following:
public function features()
{
$this->render('template', 'pages/features');
}
However, when I try to open features.php, it gives me 404. How can I fix that?
Update 1 - Class
Here is the controller's class code:
class Pages extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->model('setting_model', 'setting');
$this->load->model('order_model', 'order');
$this->load->model('page_model', 'page');
$this->load->library('form_validation');
$this->load->helper(array('inflector', 'string'));
}
public function index()
{
$settings = $this->setting->get_settings();
$data['document_price'] = $settings->document_price;
$this->render('template', 'pages/index', $data);
}
//This works fine
public function about_us()
{
$this->render('template', 'pages/about_us');
}
//Here is the problem, although it follows the same pattern as about_us()
public function features()
{
$this->render('template', 'pages/features');
}
}
As you are using $this->render I guess you are using the template library. I think you should be using:
public function features()
{
$this->template->set_template('template');
$this->template->write_view('pages/features');
$this->template->render();
}
The php files contained in /views are not directly accessible by typing in some URL. CodeIgniter is an MVC framework. That means that your URLs are mapped to your controllers and the controllers call the views.
What is the name of the class that this function is encapsulated in? Please post the entire class and not just the features() function and we can help you out. If you're working locally, the default mapping to call controllers is: http://localhost/appname/controller/function/param1/param2/etc.
The $this->render() function is not vanilla CodeIgniter syntax, you either inherited a project that is using a templating library, or, there is a sibling render() function inside the controller class.
Check your config/routes.php file as well and consider posting it.
If you want to diagnose the issue, try pinpointing by removing the call to $this->render() and instead using CodeIgniter's native $this->load->view('pages/features') function. If this works, we can be sure it's the library or render() call.
I am trying to figure a way to call a plugin during predispatch, but I am having trouble calling it last.
Basically I need to call this after all controller predispatch, before the action method is called.
Is this possible?
I tried calling the plugin lastly by passing a high value, but I know this is wrong. I am not looking to call this on the postDispatch.
$front->registerPlugin(new Plugin_Acl, 1000);
class Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(...)
{
// ... DO STUFF before any action method
}
}
The standard Zend_Controller_Action has an empty preDispatch() method which is called after preDispatch() on all front-controller plugins (which occurs before the controller is even instantiated) and then on all attached action-helpers, right before the action method itself. See the dispatch() methods on Zend_Controller_Front, Zend_Controller_Dispatcher_Standard, and Zend_Controller_Action for the flow.
So, if you want something to run at that point on all controllers, then you could create a base controller with a preDispatch() method containing the code you want to run.
I am doing that in dispatchLoopStartup()
class My_Plugins_Front extends Zend_Controller_Plugin_Abstract {
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request){
// Register ACL plugin if is admin controller (for eg.)
if($request->getControllerName() == 'admin'){
/**
* Set up your ACL
*/
}
// You can setting variable to see in all view files
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
$view->test = 'test';
}
public function dispatchLoopShutdown(){}
}
calling in predispatch seems to work for my acl:
class Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request) {
parent::preDispatch($request);
//do some stuff
}
Then it instantiated in the application.ini with the line:
resources.frontController.plugins.acl = "Controller_Plugin_Acl"
Hope this helps some...
I'm little bit new to Zend, I want to use a controller action through out the entire application automatically, I don't have a clear idea to how to use it, thought about init() method, action helpers, etc.
Then instead of simply creating controller action create controllerAction Helper . Here you can find more about it
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
My_Helper_Magic extends Zend_Controller_Action_Helper_Abstract
{
public function preDispach()
{
//code inside here will run for entire application automatically
}
}
In your bootstrap do
Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Magic());
In Response to comment .
It depends upon your 'code fragment' , If your code fragment does not required to know nothing about module , controller, action , base url then you can use Bootstrap init function
like
public function _initAlways()
{
//see how this function name is prefixed with _init hence it will be called by ZF //everytime. You can put your code fragment here
//If your code fragment depends upon some stuff like baseurl then do action controller
// registration here instead
Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Magic());
}
In Response To comment
You can save any instance of your object inside Zend_Registy and retrieve it whereever you like
Inside Bootstrap.php
public function _initSetup()
{
$object = new My_Custom_Object();
Zend_Registry::set('my_custom_object',$object);
}
Later in your view or controller do
$myObject = Zend_Registry::get('my_custom_object'); //to access it
How can I access an action controller inside controller plugin?
I need to acces it in the preDispatch method.
class My_Controller_Plugin_MyPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$actionController = ?;
}
}
I know that I'm 2 years late to the party, but since I stumbled across this while looking for it myself I figured I'd post anyway. What you're looking for is:
//$module = $request->getModuleName(); // Not everyone uses modules.
$controller = $request->getControllerName();
$action = $request->getActionName();
Caveat... This is only giving you what was requested. If you change the request after dispatch this won't be accurate anymore.
Something like this should work:
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$actionController = $this->getActionController();
}
Edit
Actually this won't work in plugin (will work in action helper).
Some possible workarounds:
subclass Zend_Controller_Action
instead accessing the controller, access the plugin from controller using plugin broker
move plugin logic to action helper
store the data as bootstrap param
store the data in the Registry