Laravel : how to maintain different sessions of a same page? - laravel

I need to put session on the same page sidebar. when user click on any button session will get for that tab. like
Search Job
Customers
etc
In the sidebar the button or not link with any page. i am creating a dialogue box for each so their is no page for the links that's why i am facing the problem. when user click any of the sidebar button the name should highlight.
DashboardController
public function disp()
{
Session::put('page', 'search-job');
Session::put('page', 'customer');
return view('front.disp');
}
layout.blade.php
#if(Session::get('page')=="search-job")
<?php $active = "active"; ?>
#else
<?php $active = ""; ?>
#endif
<li class="nav-item {{$active}}">
<a href="javascript:void(0)" class="nav-link nav-toggle">
<span class="title">SEARCH JOBS</span>
</a>
</li>
#if(Session::get('page')=="customer")
<?php $active = "active"; ?>
#else
<?php $active = ""; ?>
#endif
<li class="nav-item {{$active}}">
<a href="javascript:void(0)" class="nav-link nav-toggle">
<span class="title">Customer</span>
</a>
</li>
How to handle on this sidebar
Thanks!

as i understand from the comments you want to have different active sessions then you have 2 options like below:
put them in different indexes like this:
Session::put('page-search-job', true);
Session::put('page-customer', false);
and in view check them like this:
<li class="nav-item {{ Session::get('page-search-job') == true ? 'active' : '' }}">
<a href="javascript:void(0)" class="nav-link nav-toggle">
<span class="title">SEARCH JOBS</span>
</a>
</li>
put it in an array like this:
Session::put('page', [
"search-job"=>true,
"customer"=>false
]);
and in view check them like this:
<li class="nav-item {{ Session::get('page')['search-job'] == true ? 'active' : '' }}">
<a href="javascript:void(0)" class="nav-link nav-toggle">
<span class="title">SEARCH JOBS</span>
</a>
</li>

Related

Set Current Category to Active in laravel

i am trying to Set Current Category to active when i reload my page but unfortunately it's not working please help me thanks.
html view
<ul class='visible-links nav nav-tabs' role="tablist">
#foreach($productCategories as $productCategory)
<li>
<a class="tab-a active-a productcategory {{ Request::is('') ? 'active' : '' }}" data-id="{{$productCategory->id}}" data-toggle="tab" href="{{$productCategory->id}}" role="tab" data-toggle="tab">{{$productCategory->name}}
</a>
</li>
#endforeach
</ul>
Route
Route::get('products/get-product', 'ProductController#getproduct')->name('products.getproduct');
Route::get('/products/{slug}', 'ProductController#index')->name('products');
you can try this :
assuming this is from route Route::get('/products/{slug}'...
<ul class='visible-links nav nav-tabs' role="tablist">
#foreach($productCategories as $productCategory)
<li>
<a class="tab-a active-a productcategory
{{ request()->route('slug') == $productCategory->slug ? 'active' : '' }}"
data-id="{{$productCategory->id}}" data-toggle="tab" href="{{$productCategory->id}}"
role="tab" data-toggle="tab">{{$productCategory->name}}
</a>
</li>
#endforeach
</ul>
//Request::route() or the helper request()->route()
if you are using model binding in the controller you can use request()->route('slug')->id == $productCategory->id
make sure that active class is triggering active tab, or else the active-a is the class triggering active tab

I need to hide an specific element of a navbar depending on a rol

I have a navbar and I have 2 elements outside the login option, the thing is that I need that a certain element stays with a certain role and when I'm not logged in...
Here is my code
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#services"><i class="fa fa-search"></i> Buscar</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#about">Explorar cursos</a>
</li>
#if (Route::has('login'))
#auth
#role('Alumno')
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#about">Explorar cursos</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="{{route('miscursos')}}"> Mis cursos</a>
</li>
#endrole
#role('Administrador')
<li class="nav-item dropdown">
<a class="nav-link" href="{{ route('home') }}" >
{{ __('Tablero')}}
</a>
</li>
#endrole
The second element on the list is the one I'm having problem with because I want it when I'm browsing in the page but NOT logged in and I need when I'm logged in with an 'Alumno' role
The thing is that I don't wanna see it when I'm logged in as an administrator
You can use :
Auth::guest() for conditional when user it not logged in. So if I am getting it right, in your second list element :
<?php
#if(Auth::guest() || Auth::user()->role != 'Administrator)
// Only visible if user is NOT logged in OR logged in but not an administrator
#endif

Session flash data doesn't persist after redirect

My flash data messages are not getting passed after a redirect in Codeigniter. I have a User controller:
class User extends CI_Controller {
public function register(){
$data['title']='Register Here';
$this->form_validation->set_rules('name' , 'Name' , 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('user_name' , 'Username' , 'trim|required|max_length[32]|callback_check_username_exists'); // or is_unique[users.username]
$this->form_validation->set_rules('password', 'Password','trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('confirm_password', 'Confirm Password','trim|required|min_length[5]|max_length[12]|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]', array('is_unique'=>'Email Already Used'));
if ($this->form_validation->run()==FALSE){
$this->load->view('templates/header');
$this->load->view('users/register',$data);
$this->load->view('templates/footer');
} else {
$option=array('cost'=>12);
$encrypt_password=password_hash($this->input->post('password'),PASSWORD_BCRYPT,$option);
$this->user_model->add_user($encrypt_password);
$this->session->set_flashdata('user_registered','You are Successfully Registered and Logged In');
// This works print_r($this->session->flashdata('user_registered'));
redirect('posts');
}
}
In the User controller flashdata 'user_registered ' is getting stored when I echo it as in comment above .
My POST controller is :
class Posts extends CI_Controller {
public function index(){
$data['title']='Latest Posts';
$posts=$this->post_model->get_post();
$data['posts']=$posts;
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
My header view is :
<html>
<head>
<title>My Blog </title>
<link rel="stylesheet" href="<?php echo base_url();?>asset/css/bootstrap.min.css">
<link rel="stylesheet" href="<?php echo base_url();?>asset/css/style.css">
<script src="https://cdn.ckeditor.com/4.11.1/standard/ckeditor.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="<?php echo base_url();?>">Bloggy</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor02" aria-controls="navbarColor02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarColor02">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="<?php echo base_url();?>">Home </a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>posts">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>category">Categories</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>about">About</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>user/register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>posts/create">Create Post</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>category/create">Create Category</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<!-- Flash Data -->
<?php if($this->session->flashdata('user_registered')): ?>
<?php echo $this->session->flashdata('user_registered');?>
<?php else :?>
<?php echo ' no value '; ?>
<?php /*echo '<p class="alert alert-success">'.$this->session->flashdata('user_registered').'</p>'; */?>
<?php endif; ?>
In the header view the flashdata user_registered is showing up with no value. There is only one redirect and I should be able to access the flash data.
On further investigation I found my sessions are not working at all. I have auto loaded the sessions library and in the browser I am seeing ci_session as a cookie but I tested out both userdata and flashdata are not working after redirect. Flash data should work after for one redirect as I have been doing this for a long time. My previous application which worked perfectly is also failing because sessions are not getting stored.
I created a test code like below:
class User extends CI_Controller {
public function index (){
$sex='M';
$data1['user']=$this->user_model->get_all_users($sex);
$this->session->set_userdata('test','data');
$this->session->set_flashdata('flash', 'data');
print_r($_SESSION); exit();
redirect('projects');
}
I get Array ( [__ci_last_regenerate] => 1547034241 [test] => data [flash] => data [__ci_vars] => Array ( [flash] => new ) ) .
Now I echo the session in my projects controller :
<?php
class Projects extends CI_Controller {
public function index()
{
print_r($_SESSION); exit();
$result= $this->project->get_projects();
$data['result']=$result ;
$this->load->view('pages/projects',$data);
}
}
I just get this : Array ( [__ci_last_regenerate] => 1547034384 ) .
My config.php is $config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
flash data is session which will available at page only.. when it get freshh request on server it empty flash dat ... i.e your redirect unset the flash data... set user_data to get your message ... and unset it when you have it like
$this->session->userdata('user_registered','You are Successfully Registered and Logged In');
unset it at view
<?php
if($this->session->userdata('user_registered')){
echo $this->session->userdata('user_registered');
$this->session->unset_userdata('user_registered');
}
?>

How to keep navigation menu state between page navigations and refresh

Im using laravel to develop a website. There is a side panel for navigating between the different pages of the website. Some pages are grouped together in an accordion (or dropdown). When I click on one of the options within the accordion it navigates to the page but the accordion closes immediately. How do I keep it open??
here is how the accordion is
<li class="treeview">
<a href="#">
<span>Group of pages</span>
<ul class="treeview-menu"
style="display: none;">
<li>
Page 1
</li>
<li>
Page 2
</li>
</ul>
</li>
Any help is appreciated.
ps. It might be worth it to add that I am using AdminLTE as a template.
Need to add Active class in li
<li class="treeview {{ ((Request::is('a-page')) || (Request::is('a-second-page')) ? 'active' : '') }}">
<a href="#">
<span>Group of pages</span>
<ul class="treeview-menu">
<li class="{{ ((Request::is('a-page')) ? 'active' : '') }}">
Page 1
</li>
<li class="{{ ((Request::is('a-second-page')) ? 'active' : '') }}">
Page 2
</li>
</ul>
</li>
OR
<li class="treeview {{ ((Route::currentRouteName() == 'a-route') || (Route::currentRouteName() == 'a-second-route') ? 'active' : '') }}">
<a href="#">
<span>Group of pages</span>
<ul class="treeview-menu">
<li class="{{ ((Route::currentRouteName() == 'a-route') ? 'active' : '') }}">
Page 1
</li>
<li class="{{ ((Route::currentRouteName() == 'a-second-route') ? 'active' : '') }}">
Page 2
</li>
</ul>
</li>

Magento hide navigation menu item from guest

I'm running a magento custom template on magento community edition 1.7. How do I hide $custom_tab3 from guests? I want that tab to only show up for logged in users. Please see code below. Any help is super highly appreciated!
<ul id="nav">
<?php if (Mage::getStoreConfig('celebritysettings/celebritysettings_header/navigation_home')): ?>
<li class="level0 level-top">
<span><?php echo $this->__('How It Works'); ?></span>
</li>
<?php endif; ?>
<?php
echo $_menu;
$custom_tab = Mage::getModel('cms/block')->load('celebrity_navigation_block');
if($custom_tab->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="'.$this->getBaseUrl().'gift" class="level-top">
<span>'.$custom_tab->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block')->toHtml().'</div>
</li>';
}
$custom_tab2 = Mage::getModel('cms/block')->load('celebrity_navigation_block2');
if($custom_tab2->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block" >
<a href="'.$this->getBaseUrl().'plans" class="level-top">
<span>'.$custom_tab2->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block2')->toHtml().'</div>
</li>';
}
$custom_tab3 = Mage::getModel('cms/block')->load('celebrity_navigation_block3');
if($custom_tab3->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="'.$this->getBaseUrl().'showroom" class="level-top">
<span>'.$custom_tab3->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block3')->toHtml().'</div>
</li>';
}
$custom_tab4 = Mage::getModel('cms/block')->load('celebrity_navigation_block4');
if($custom_tab4->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="'.$this->getBaseUrl().'magazine" class="level-top">
<span>'.$custom_tab4->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block4')->toHtml().'</div>
</li>';
}
?>
</ul>
You can use Mage::getSingleton('customer/session')->isLoggedIn() to check whether the current user is logged in. In your code you can use it like this:
$custom_tab3 = Mage::getModel('cms/block')->load('celebrity_navigation_block3');
if($custom_tab3->getIsActive() && Mage::getSingleton('customer/session')->isLoggedIn()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="'.$this->getBaseUrl().'showroom" class="level-top">
<span>'.$custom_tab3->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block3')->toHtml().'</div>
</li>';
}
or even
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$custom_tab3 = Mage::getModel('cms/block')->load('celebrity_navigation_block3');
if($custom_tab3->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="'.$this->getBaseUrl().'showroom" class="level-top">
<span>'.$custom_tab3->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block3')->toHtml().'</div>
</li>';
}
}
to prevent unnecessary retrieval of the cms/block model.

Resources