I have this layout file called menuAdmin.
I wish to, each time a given controller and a given action is active, to show the "li" element with a specific class.
So, I have the following on my menuAdmin.php :
<li <?php echo ($this->controller == "d" && $this->action == "a") ? "class='selectedMenuItem'" : ''; ?>>Aaaa Dddd</li>
I get nothing with this, and if I dump:
var_dump($this->controller); and var_dump($this->action); I get NULL NULL
So I believe Zend don't trigger those at that point.
Question:
How can I accomplish such a task? Should I follow this path? If so, how will my menuAdmin layout know about what controller and action is in place?
Update:
menuAdmin.php is a layout file, inside Layouts folder on Zend structure.
This is a large application and the structure in place is already like this - using layout files as menus where this is just one of them.
So $this->controller and $this->action only work inside the controller, OR if I explicitly pass it to the view. On this case, however, I would like to call it on the layout. Why there? Because by doing so, I can make one change and allow that change to be replicated all over the views that use this layout.
Regarding the above clarifications, could your answers change ?
Update 2:
I don't know if this is relevant or not but, all this menuadmin layout is called from a main layout file "layouts/main.php" and there we have: <?php echo $this->render("menuadmin.php"); ?>
Thanks again
Depending on what menuAdmin.php is you can get the controller and action in a variety of ways.
If your file is a controller you can do one of the following, they all do the same thing
$controller = $this->getRequest()->controller;
$controller = $this->getRequest()->getParam("controller");
$controller = $this->getRequest()->getControllerName();
Ideally you should use Zend_Navigation to do this though.
You should instead use Zend_Navigation as this is a built-in feature.
Edit: To answer your question about the null "controller" and "action" values; unless you have set these as view parameters from a controller or something else at the controller level (helper, plugin, etc), of course they will be empty.
Related
I'm trying to pass a variable from the master layout to the view, but I'm having no luck.
layout.phtml
$this->hadMessages = true;
myview.phtml
var_dump($this->hadMessages);
The var_dump always comes back with NULL. From what I've read, the layout is a view too, so it should be in the same context, right?
I'm using Zend Framework 1.11.
The layout is rendered after the view, so that's why this doesn't work. Depending on what you are trying to do you might be able to achieve the desired effect with the help of a controller plugin.
There is no way to pass variable from layout to view, because according to MVC pattern you shouldn't have tasks like this. Wether layout contains some messages or not should be decided on controller or bootstrap level, not in layout itself.
It means in controller you should make all needed assignments like $this->view->layout_messages_shown = true and get this variable value both in layout and view like echo ( $this->layout_messages_shown ? "messages shown" : "messages hidden" )
I want to create my custom theme in CakePHP. Where can I save my block in CakePHP directory structure that will contain all my menu links? And how can I fetch the file from View\Themed\MyTheme\Layouts\default.ctp?
Either use Elements (if your navbar shows on every single view) or use View Blocks (if navbar only shows on some views).
In your default.ctp file you would just do..
echo $this->element('navbar');
Which would render Views/Elements/navbar.ctp onto Views/Layouts/default.ctp (if you are using the default layout).
To access: View\Themed\MyTheme\Layouts\default.ctp, in (Cake 2.1+), you must tell cake which theme you want to use like so:
public $theme = 'MyTheme';
// or override in an action:
$this->theme = 'MyTheme';
Then:
$this->layout = 'default';
will refer to your View\Themed\MyTheme\Layouts\default.ctp.
If cake can't find the requested view file in MyTheme, it will fallback to app/View to find it.
In this way you can override views in your theme as needed.
So if you place your menu in View/Elements/menu.ctp, all of your themes will be able to access it. if you want to overwrite it for MyTheme, simply create an Elements/menu.ctp within MyTheme.
The process is slightly different with previous versions of cake if I recall.
Just place it under /App/Views/Layouts. If you want to use it everywhere, call it default.ctp, it will automatically be used. Otherwise, give it a different name, and then in the controller do:
public function some_action() {
$this->layout = 'mylayout';
}
This will display some_action using your layout instead of the default.
I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?
I want to add a view helper path in an existing project. To do this I have added the following line to my application.ini:
resources.view[] =
And in my bootstrap file:
$this->bootstrap("view");
$view = $this->getResource("view");
$view->addHelperPath(APPLICATION_PATH . "/../library/MyPath", MyNamespace");
Now I am indeed able to add view helpers to my path, so no problem there.
However, variables that I have added to the view in my Action Helpers are suddenly no longer accessible inside my views. I can retreive them inside my layout as usual so I know they get assigned properly.
I assign a variable in my Action Helper in the postDispatch:
$view = $this->getActionController()->view;
$view->myVar = $this->var;
Then in my layout
Zend_Debug::dump( $this->myVar );
results in: (string) "myVar contents"
And in my view
Zend_Debug::dump( $this->myVar );
results in: null
Since this is an existing project I need a general solution that I can use in either my bootstrap or application.ini
You can add paths to your view from application.ini:
resources.view.helperPath.MyPrefix = "/path/to/helpers"
Hopefully this will fix the problem. I'm assuming that the way you do it, an existing view-instance is somehow overwritten, which destroy previous changes. You could try fetching the view from the frontcontroller, set the helper-path, and pass it back to the frontcontroller.
After some research this is what I found out.
Edit: Removed text concerning differences between view variables and layout variables - see comments by #pieter and #david weinraub
The postDispatch of the Action Helper fires AFTER the view is rendered, but BEFORE the layout is rendered. Therefore, any variables assigned to the view in the postDispatch of the action helper were unavailable in my views, they didn't exist yet.
I am very curious why this behaviour occurs ONLY when actively bootstrapping the view? I will leave the question unanswered, maybe someone can clarify this.
I call an action helper in one of my views using the following code
echo $this->action('foo', 'bar');
The fooAction in the barController does its thing and outputs a list of pages. However, the list has the layout in the output again, which is mightily irritating. If I disable the layout in the fooAction, this causes layout to be completely disabled on the live side, as well.
I'm vexed. I could just create a view helper, and there are many ways around this, but out of curiousity I was wondering if anyone had a solution to this.
From the ZF Reference Guide on Action ViewHelper
The API for the Action view helper follows that of most MVC components that invoke controller actions: action($action, $controller, $module = null, array $params = array()). $action and $controller are required; if no module is specified, the default module is assumed.
Modify your controller to accept a param that controls whether the action should disable the layout. When using the action helper, pass this control flag.
On a sidenote: using the Action ViewHelper is considered bad practise as it will go through the entire dispatch process again and this will slow down your app. If possible, try to access the model directly.