I have a Site Layout with a top nav bar, I need to set the class to active depending on the controller which changes when the user clicks on different pages.
views->index.phtml
<li class='<?php echo (isset($this->controllerName) && $this->controllerName == 'about') ? 'active' : '' ?>'>About Us</li>
<li class='<?php echo (isset($this->controllerName) && $this->controllerName == 'services') ? 'active' : '' ?>'>Member Sevices</li>
I was hoping that Phalcon had a view function or maybe something I can put in the bootstrap so it worked for all pages with out me having to remember to set the controllerName variable in each Controller manually.
Inside your view access your router service :
$this->router->getControllerName()
$this->router->getActionName()
or if you are using volt you can use the short hand
router.getControllerName()
router.getActionName()
That will work for your use case, but if you find that your menu logic becomes too unwieldy, there are several methods to achieve what you are attempting to do.
The easiest I find is to use javascript, but that doesn't have a fallback if they have js disabled(1%). If you want to cover 100% of your audience you could use a method like the Phalcon team did in INVO using an element library: https://github.com/phalcon/invo/blob/master/app/library/Elements.php
Related
I have a basic application being baked by CakePHP.
Now i want to add a new action'listjobs' in my model controller class and a corresponding 'listjobs' view in the /Template/Job/listjobs.ctp.
From my model's index view i added one more action in the side navigation bar.
like this
<li><?= $this->Html->link(__('View Jobs'),['action' => 'listjobs']) ?></li>
When i click on the link 'View Jobs' control is directed to the action method of my model's controller but its not taking the list jobs 's view.
Code for my action method
public function listjobs()
{
$this->log("inside list jobs",'debug');
$this->render('listjobs');
}
The listjobs.ctp contains a very basic code as following
<div class="actions columns large-2 medium-3">
<h3><?= __('Actions') ?></h3>
<ul class="side-nav">
<li>< Git Hub >
</li>
<li>< Stack overflow >
</li>
</ul>
</div>
Not able to find out why the view is not rendered rather the index view is getting rendered.
Links loaded according to chrome console in order
(1) http://localhost/myjobs/job/listjobs
(2) http://localhost/myjobs/job
So (1) should have been loaded with the view..it should not been redirected to (2)
Regards,
Saurav
I think you need to use like this.
<li><?php echo $this->Html->link('View Jobs',array('controller' => 'MyModel', 'action' => 'listjobs')); ?></li>
Your view will be.
public function listjobs()
{
$this->log("inside list jobs",'debug');
}
I think you don't need to render, your view will be render by default, you will need to send data like that $this->set(compact('VarName'));
Secondly $this->log("inside list jobs",'debug'); if log is a method then you need to set action, then it will be.
$this->setAction('log');
Try, if you get error, then let me know. If your work proper then, you view will work.
http://localhost/myjobs/job/listjobs
this link defines.
http://localhost/SiteName/ControllerName/ActionName/Parameter
Get review and study this page.
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html
Thanks
ok...i did some debugging of the cakephp framework code.
I was using an auth component for my login functionality.
I found that the newly added action was not allowed to pass the through filter of auth component. so i added the newly added action in the beforefilter method of appcontroller like this. i guess the crud operations are already added in the filter by default.
$this->Auth->allow(['index', 'view', 'display','listjobs','add','delete']);
cheers,
Saurav
I'm organizing my website in sections, which should be visible at first glance on my navigation bar (with an active class on the appropriate link). At the moment, I'm checking for each link in the navbar if the current URL matches the one for the link, but it's only working for 1 URL in each case. It should be like this :
article ----------------> article section
article/create -----> article section
article/edit --------> article section
forum -------------------> forum section
forum/post/12345 -> forum section
Since all my "sections" use controllers, I was thinking maybe I could implement a way (maybe using the constructor) to pass a variable (section) to all the views returned by a controller, so that my layout can have access to it and set the active class on the proper link.
But I don't want to have to do return View::make('myView')->with('section', $this->section); everytime
Anyone knows how to achieve that ? Thanks.
You should use Request::segment(1) to compare it with section.
If your url is article/create and you use Request::segment(1) it will return you article and not article/create
And in fact, you don't have to pass anything to Blade in this case, because it should be visible:
#if (Request::segment(1) == 'article')
class="active"
#endif
You could also share it between all views, this way its easier to change the segment later if that changes and saves you from having to edit Request::segment(1) in all your views (if you have more)
Use:
View::share('section', Request::segment(1));
Then in every view get the value with: $section
I am using Laravel for a CMS Application, I have stored the navigation menu in Cache, Since It's generated dynamically from the database and every time the navigation menu is updated in Database, The cache will be updated as well.
The only issue i have is, Though i can access the navigation menu easily, I want to set active class on the link of the page being accessed and this is really driving me mad.
This is how i have stored the nav in cache
Home
About Us
Services
And this is how i access the navigation within the page.blade.php file
{{ Navigation::display_main_menu() }}, This function checks whether a cache exists or not, If not then it creates the cache and then returns the value.
But the navigation gets displayed exactly like this with php tags and not with the class (which i want to see), I even tried changing how i store nav to Home but even this gets displayed exactly as it is.
What can i do to execute the php code or the blade syntax when the navigation is being rendered.
Please point me in the right direction. Any help is greatly appreciated.
You may add a new macro in HTML class, simply add this in a file (macros.php) and store it inside app/start directory then include it from the app/startglobal.php file using require/include:
HTML::macro('menuItem', function($name, $title = null, $parameters = array(), $attributes = array()){
$currentUrl = URL::current() == url() ? url('home') : URL::current();
$active = ( $currentUrl == URL::route($name, $parameters) ) ? ' class="active"':'';
return '<li'.$active.'>' . HTML::linkRoute($name, $title, $parameters, $attributes) . '</li>';
});
In your view you may generate the menu items using something like this:
<ul>
{{ HTML::menuItem('page.home', 'Home', array('home')) }}
</ul>
Here page.home is the route name assigned to / when declaring the route and yes in this case routes should have name, for example:
Route::get('/', array('uses' => 'PageController#home', 'as' => 'page.home'));
In the menuItem macro the second parameter (Home) is the menu title to show and the third parameter is an optional parameter if has any. So for example, your About Us page has the parameter about-us and to create the menu item you may use this:
{{ HTML::menuItem('page.about', 'About Us', array('about-us')) }}
This is a possible solution and there may be other ways.
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 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.