Hy,
I have managed to make a top menu bar in Zend Framework and the code is placed in layouts/scripts/layout.phtml, and i have the followind items: Item1, Item2, Item3, Login, Register. What i want is that when somebody logs in, to change the last 2 elements, replace Login and Register with Logout. Can someone help me with something please? I readed something about placeholders, but i don`t understand them clearly.
Thank you.
Since it sounds like you are using a view-script (rather Zend_Navigation) for your menu, it should relatively straightforward to include a check as follows right in layouts/scripts/layout.phtml:
// Render your static menu elements
// render, render
// Now handle the dynamic part
if (!Zend_Auth::getInstance()->hasIdentity()){
// Render the `Register` and `Login` links.
} else {
// Render your `Logout` link.
}
Related
I am trying to create a module which has both frontend and backend functionality. Like I need to ask for the city in the home page when the store loads. And all the available cities are entered/managed in backend admin panel.
Before I used to write for only backend things, frontend seems little confusing.
There is a design folder which is completely for theme development.
All the example are little different(https://www.mageplaza.com/magento-2-module-development/,http://inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2/]2), they have routes.xml, where route_id, and all are defined, but here I don't need any extra route. Need some additional tweaks in frontend pages.
I created module V_name/M_name/adminhtml/block controllers etc view ...
Guide me how to create a module, which has both front end and backend connection, cities should be entered in admin, they should show on the frontend homepage.
For now, I only managed to edit home page content CMS page by adding some HTML which shows a popup with a dropdown for cities when the page loads.
Since you already have the back-end figured out I will focus on front-end. Also, since all you need to do is populate a list that you already have created this should be easy. I did something like this before and I found it easier to just use JSON to query a list of, in your case the cities, and populate the drop down. I don't believe this is the most 'MVP/proper' way to go, but it is less work then the other ways. (At least for me it is. I always prefer the JavaScript option since it allows for easy future page customization.)
To use the JSON method you need to create a Block with a method like the one below. You will see that you will also have to create a Resource Model (I'm not going to go over creating the Resource Model or the details of Blocks since there are much better resources than me already online that will go into every single detail you need.). Once this is complete you can access the data straight from the .phtml page in an easy to use JSON array.
First you need to make sure you are now structuring your Modules properly. The new Block below should be in a structure like this...
app/code/<VENDOR>/<MODULE>/Block/Wrapper.php (or whatever you name it)
The admin Blocks should be in the structure below, which it sounds like you are already know how to do.
app/code/<VENDOR>/<MODULE>/Block/Adminhtml
Create your Block and add a method to create a JOSN array like below...
public function getCityList()
{
$city_array = array();
/** #var \<VENDOR>\<MODULE>\Model\ResourceModel\City\Collection $collection */
$collection = $this->_cityCollectionFactory->create();
$collection->addFieldToFilter('active','1')->addFieldToSelect(['city_id', 'city']);
$collection->getSelect()->order(array('city ASC', 'city_id ASC'));
$count = 0;
foreach ($collection as $model)
{
$city_array["$count"] = $model->getData();
$count++;
}
return \Zend_Json::encode($city_array);
}
FYI... The foreach loop in the code above is weird and uses $count because I needed to do some tricky things to get something to work.
Then you can create the Block in your .phtml file to access the data via javascript.
<?php
$block_obj = $block->getLayout()->createBlock('<VENDOR>\<MODULE>\Block\Wrapper');
?>
<script type="text/javascript">
window.citylistJson = <?php echo $block_obj->getCityList() ?>;
</script>
I'm looking for some feedback on using If/Else for my menu navigation or links to resources. I have done a few quick searches and have found items relating to using PHP to keep track of the current page and apply specific CSS rather than implementing the menu itself.
What I'm trying to do is have the menu change my content div based on the menu. So if "home" is clicked, the div changes to home and if "about" is clicked, the div changes to about.
What I've done so far is to create a variable 'cl' that I set using the id attribute in each div. In my index page I use the following:
//if cl is not set include default content div
if(!isset($_GET['cl']))
{
//include main div
}
//if cl is set, check value and include the correct div
//anything that does not match a hard-coded cl value --SHOULD-- default to main
//div...may change this to be a custom error page
if(isset($_GET['cl']))
{
if($_GET['cl']=="content-main")
{
//include main content div
}
elseif($_GET['cl']=="content-anotherPage")
{
//include another page div
}
else
{
//include main content or possibly custom error
}
}
I have more elseif blocks thrown in for other menu items, but left this small to just show what I'm doing.
Is this method a valid approach? Are there any basic things I'm missing that I should be aware of? Is my If/else block secured? (By secure I mean changing the value of URL should just revert to the default content div. I tried placing a basic javascript alert script into the URL and it did default to the main page, but not sure if my basic test relates to actual security).
First time poster...please let me know if I have not followed etiquette and I will gladly correct!
Mike
instead of bunch of if/elseif's I'd rather use switch/case syntax:
switch( $_GET['cl'] ) {
case "content-main":
...
break;
default:
...
break;
}
If you have lots of conditions, I would use a switch statement instead http://www.w3schools.com/php/php_switch.asp as it's much faster. If you only have a few conditions like less than 5 or 6, if/else is fine.
For my Codeigniter site, I started by making a view for each controller situation. This was impractical, as it would require going back to the code for each to make a change. So I changed approach and operated on a 'default' controller with optional fields. I then thought I could load special views as needed into it.
I put together this view with optional fields with fields for $title, $search_bar on/off etc. However, now came the content area. I was able to load more views into this default view using:
$data['content_views'][]='blocks/login';
$this->load->view('default/a', $data);
and in the 'default'view:
if(isset($content_views)&& (is_array($content_views)))
{
foreach($content_views as $content_view)
{
$this->load->view(&$content_view);
}
}
(and that works fine)
Two questions:
Am I making things to complex? Is this an accepted way of doing this? Or have I misunderstood the functioning of a view and how they are intended to work?
I want a way to mix the $content_view, i.e. a block of text, then a view. I'm not quite sure as to how to proceed. Say I want a message first, then a view, then more text. This method will only accept views.
Can anybody help me create this flexible approach?
Yeah I would say you're making things a little complex. While I may not be following your description well enough to know precisely how to respond, I can tell you how I do it:
First the whole site is run through a template so the header and footer are the container file and all views needed within the site are rendered as page type views - like an article page, a gallery page, etc. Components are loaded and passed to the views as strings:
$content['sidebar'] = $this->load->view('components/sidebar', $data, true);
That last true says to render as string.
Essentially, this means the page views are pretty much html with php echoing out the necessary elements. No views calling other views, which is how I read your example.
CI loads views progressively, so your controller can output like so:
$this->load->view('header', $header_data);
$view_data['sidebar'] = $this->load->view('components/sidebar', $sidebar_data, true);
$this->load->view('content', $view_data);
$this->load->view('footer', $footer_data);
and in content view, handle the sidebar like so:
<?php if(isset($sidebar)): ?>
<nav>
<?php echo $sidebar; ?>
</nav>
<?php endif; ?>
And, assuming you populate those arrays for each view it will render header, then content, then footer. And also render sidebar if it is present.
So combining everything, I'm basically saying you can load in sections in your controllers progressively, passing sub-views as strings to whichever section makes sense. That keeps your view controlling in the controller where it belongs and not in the view files themselves. In my experience, I have not had to write a site that was so complex that this construct wasn't perfectly suitable if the site is planned well.
[See update at the end]
I'm working with Zend framework, in PHP and I have some difficulties with Zend Navigation. It's my first question here, so if something's wrong with it, just tell me and I'll correct it.
I have a menu looking like this in my application
Home
Login
Logout
Member's Page
I have a navigation xml file containing my menu.
<nav>
<home>
<label>Home</label>
<uri>/</uri>
</home>
<login>
<label>Login</label>
<uri>/index/login</uri>
</login>
<logout>
<label>Logout</label>
<uri>/index/logout</uri>
</logout>
<member>
<label>Member's Page</label>
<uri>/index/member</uri>
</member>
</nav>
Also a menu.phtml containing this
<div class="top-level">
<?php
foreach ($this->container as $page) {
if ($page->isVisible()) {
if ($page->isActive(true)) {
if ($page->isActive(false)
)$page->setClass("active");
else
$page->setClass("open");
echo $this->navigation()->menu()->htmlify($page);
//... the same continue for the 3 menu level
Finally, in my layout.phtml, I have this to render the menu
<?php
$partial = array('menu.phtml', 'default');
$this->navigation()->menu()->setPartial($partial);
echo $this->navigation()->menu()->render();
?>
For now, my menu works good, but I can't have Login and Logout always displayed in my menu. So, what I need to do, is to hide Login when I'm logged in, and to hide Logout when I'm logged out. It looked pretty simple when I started, and it still does, but I can't make it work. I don't know how and if I can hide and show item depending on logged users. I really need to make it work, because I will need to hide/display other items in the future.
So is there a way of doing that ?
Thanks !
EDIT :
I'm currently not using Zend::Auth or Zend_ACL for roles and authorization. If I want to know if the user is logged in or not, I have a token in the session that is valid only when the user is logged in. I'd like my menu to work without changing that if it's possible.
UPDATE :
I had it working in another way than those suggested. I'm really not sure it's a clean way, but it's doing the job for now. So now, my xml navigation file look like
<menuAnonymous>
<home>
<label>Login</label>
<uri>/login</uri>
</home>
</menuAnonymous>
<menuLogged>
<home>
<label>Logout</label>
<uri>/Logout</uri>
</home>
</menuLogged>
I initialize both in my bootstrap like this.
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'menuNotLogged');
$container = new Zend_Navigation($config);
Zend_Registry::set('main',$container);
And then, in my layout, I look at my token and display the menu depending on that.
if ($tokenValid) {
echo $this->navigation()->menu()->render(Zend_Registry::get('main'));
} else {
echo $this->navigation()->menu()->render(Zend_Registry::get('logged'));
}
So it's working like I want now, but I still want to do it cleaner, so if you have any suggestions to help me ... thank you !
You should have a look at the "Leveraging Zend_Navigator" webinar on http://www.zend.com/en/resources/webinars/framework. It explains how you can tie your navigation to specifiec roles/ACL.
I updated my question with the solution I used, maybe I'll find a better way to do it later but for now this is it. I didn't find the solution here, that's why I'm answering my own question. Thanks for your help !
I am trying to create a wordpress plugin admin menu. The problem that I am running into is with the menus. I am trying to add a page in the admin without actually adding the menu link. So for example I want to have a menu called test then I want to have some extra pages but I don't want physical links to them because they are only going to be used when there is an id to pass to them. is this possible and if so please someone explain because i can't seem to figure it out.
Yes. In your callback function for the admin page, just write out different sections and use conditional checks to display the right content. Then, under the page's title, add a <ul> with the class subsubsub containing the links to take the user to the right place. Something like this:
function my_awesome_admin_page(){
echo '<h2>My Title</h2>';
echo '<ul class="subsubsub"> <li>Foo</li> <li>Bar</li> </ul>';
if($_GET['foo'] != 'bar'){
//You're on the first page
} else {
//You're on the second page
}
}
I forget what the class is to signify the current subpage, but you can take a look on the 'Add Plugin' admin page. I think it's selected.