Menu in CodeIgniter with session - codeigniter

I'm creating a simple application in CI using sessions.
The nav menu is loaded in template.php file.
I want to know what is the best way to make a dinamic menu based in $this->session-userdata('logued').
If is not logued I want to show only "Log in" option in menu, but if is logued I want to load the complete menu options.
What is the best way in CI to do it?
Thanks.

You have the solution right there:
"If is not logued I want to show only "Log in" option in menu, but if is logued I want to load the complete menu options."
Just convert this to a PHP code and you're good to go. There's nothing special CodeIgniter can do for you in this case (I assume you can create separate menus as CI views, but that's about it)... Just check the session and show the appropriate menu based on whether the user is logged in:
View:
if ( $this->session->userdata('logued') ) {
// show real menu
}
else {
// show "log in" menu
}
UPDATE
If you want to do this in your controller, there are many ways as well. For example, you can load different views (if that's your structure):
Controller:
if ( $this->session->userdata('logued') ) {
$menu = 'full_menu'; // or whatever the view name is
}
else {
$menu = 'log_in_menu';
}
// your views, for example
$this->load->view('header');
$this->load->view($menu);
$this->load->view('content');
// etc
Or you can use $data['menu'] = 'full_menu'; and then pass it to the header view (or the only/template view): $this->load->view('header', $data);...etc. Plenty of options to do this.

Just add some information, if you want to use navigation with different access role using SESSION.
In my case, there are 2 access role. First is admin and the other is user.
The admin can manage 3 menus. And the user only can manage 2 menus. Here is my code
<?php if (($_SESSION['logged_in']) && $_SESSION['is_admin'] === true): ?>
<ul id="sidebarnav">
<li>
<a class="has-arrow waves-effect waves-dark" href="#" aria-expanded="false"><i class="mdi mdi-laptop-windows"></i><span class="">Main Menu</span></a>
<ul aria-expanded="false" class="collapse">
<li>
<a class="waves-effect waves-dark" href="<?php echo base_url()?>admin/dashboard" aria-expanded="false"><i class="mdi mdi-gauge"></i><span class="hide-menu">Dashboard</span></a>
</li>
<li>
<a class="waves-effect waves-dark" href="<?php echo base_url()?>admin/listpendaftaran" aria-expanded="false"><i class="mdi mdi-security-home"></i><span class="hide-menu">Pendaftaran</span></a>
</li>
<li>
<a class="waves-effect waves-dark" href="<?php echo base_url()?>penjadwalan" aria-expanded="false"><i class="mdi mdi-av-timer"></i><span class="hide-menu">Penjadwalan</span></a>
</li>
</ul>
</li>
</ul>
<?php elseif (($_SESSION['logged_in']) && $_SESSION['is_admin'] === false): ?>
<ul id="sidebarnav">
<li>
<a class="has-arrow waves-effect waves-dark" href="#" aria-expanded="false"><i class="mdi mdi-laptop-windows"></i><span class="">Main Menu</span></a>
<ul aria-expanded="false" class="collapse">
<li>
<a class="waves-effect waves-dark" href="<?php echo base_url()?>admin/dashboard" aria-expanded="false"><i class="mdi mdi-gauge"></i><span class="hide-menu">Dashboard</span></a>
</li>
<li>
<a class="waves-effect waves-dark" href="<?php echo base_url()?>penjadwalan" aria-expanded="false"><i class="mdi mdi-av-timer"></i><span class="hide-menu">Penjadwalan</span></a>
</li>
</ul>
</li>
</ul>
<?php endif;?>
Explanation :
When the user has logged in and has true value and stored into ($_SESSION['logged_in'] && the admin has the value of 1 then the menu will displayed into the admin menu (contains 3 menus).
And then, if the user has logged in and has true value and stored into ($_SESSION['logged_in'] && the admin has the value of 0then the menu will displayed into the users menu (contains 2 menus).

Related

Why opening bootstrap modal using wire:click and dispatch event Blocks Scrolling in admin menu

I have a problem when I use the following code for adding and displaying:
<a href="javascript:void(0)" data-toggle="modal" data-target="#modalYear">
<span class="sub-item">Add year</span>
</a>
<a href="javascript:void(0)" data-toggle="modal" data-target="#ListYear">
<span class="sub-item">Listyear</span>
</a>
Nothing gets blocked but the problem comes when I want to use the edit option the admin menu stops scrolling, this is where the problem starts and also when I use this :
<a href="javascript:void(0)" wire:click="ShowReportModal('add')">
span class="sub-item">Add Year</span>
</a>
the modal will show up but when I close it the admin menu stops scrolling.
After so many tries I figured out that the problem is using wire:click to call the modal for adding, editing and displaying.
this is my component:
public function ShowReportModal($action)
{
if($action == 'add')
{
$this->dispatchBrowserEvent('ModalYearVisible') ;
}
else{
$this->dispatchBrowserEvent('ModalListVisible') ;
}
}
Here is the admin menu code:
<li class="nav-item">
<a data-toggle="collapse" href="#annee">
<i class="far fa-calendar-alt"></i>
<p>Scholar year</p>
<span class="caret"></span>
</a>
<div class="collapse" id="annee">
<ul class="nav nav-collapse">
<li>
<a href="javascript:void(0)" wire:click="ShowReportModal('add')">
<span class="sub-item">Add year</span>
</a>
</a>
</li>
<li>
<a href="javascript:void(0)" wire:click="ShowReportModal('list')">
<span class="sub-item">Years List</span>
</a>
</li>
</ul>
</div>
</li>
Any help would be appreciated and also why is this happening?

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

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>

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

Semantic-ui integration with Laravel 5.7

Semantic-ui is integrated with my laravel app. All buttons are working but Menubar is not working i.e whatever code I copied from documentation it is showing like that only.
I have linked these files -
<link rel="stylesheet" type="text/css" href="{{asset('semantic/dist/semantic.min.css')}}">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="{{asset('semantic/dist/semantic.min.js')}}"></script>
<div class="ui secondary menu">
<a class="active item"> Home </a>
<a class="item"> Messages </a>
<a class="item"> Friends </a>
<div class="right menu">
<div class="item">
<div class="ui icon input">
<input type="text" placeholder="Search...">
<i class="search link icon"></i>
</div>
</div>
<a class="ui item"> Logout </a>
</div>
By default Home is active but when i click or focus on another item i.e messages or Friends , it is not showing as active otherwise hovering effect is working fine.
As you see, only the first menu item is marked as active in your code, so why do you expect any other menu items to be marked as active when you click on them?
<a class="active item"> Home </a>
You should detect which menu item is active when you render the menu and set active class to the necessary item.
So, for example, when you are on "Home" page your menu should look like this:
<a class="active item"> Home </a>
<a class="item"> Messages </a>
<a class="item"> Friends </a>
But when your are on the "Friends" page your menu should look like this instead:
<a class="item"> Home </a>
<a class="item"> Messages </a>
<a class="active item"> Friends </a>
And everything else depends on your implementation (either it's PHP or other server-side language if you generate your menu or JavaScript if you toggle its state in the browser).

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

Resources