CodeIgniter 4 Populating menu from database on View Layout - php

I'm new in CodeIgniter in general. I have a view layout, on which I have navigation menu bar in it. I want to populate the dropdown menu with categories I've saved in database.
Here's snippet of my layout (View\layouts\frontend.php):
<nav class="navbar navbar-expand-md sticky-top">
<div class="collapse navbar-collapse flex-column " id="navbar">
<!-- Brand bar -->
<a class="navbar-brand ml-3" href="#">
<span class="">Test Site</span>
</a>
<!-- Internal links Menu navbar -->
<ul class="navbar-nav justify-content-center w-100 px-3">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Categories
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">News</a>
<a class="dropdown-item" href="#">Event</a>
<a class="dropdown-item" href="#">Announcement</a>
</div>
</li>
</ul>
</nav>
Now, I understand that on every views that Extends that layout, I can just do query on controller and return the view with data. Here's what I've done so far and it works.
Home Controller:
use App\Models\CategoryModel;
class Home extends BaseController
{
public function index()
{
$categoryModel = new CategoryModel();
$data['categories'] = $categoryModel->orderBy('id', 'ASC')->findAll();
return view('frontend/home_page', $data);
}
}
Part of layout:
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Categories
</a>
<?php if ($categories) : ?>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<?php foreach ($categories as $category) : ?>
<a class="dropdown-item" href="#"><?php echo $category['name'] ?></a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</li>
But it looks like I have to do that on all controllers' functions that renders my views that use my layout (frontend.php).
What I want to ask is, is there a possibility to just make a function/class/something to call so it's only done one time? What is the best practice in this situation?

If
$categoryModel = new CategoryModel();
$data['categories'] = $categoryModel->orderBy('id', 'ASC')->findAll();
is something that you plan on calling in multiple methods within that Controller, then yes it would be considered best practice to push that code off into something reusable.
You could ostensibly define this function in several different places, but in this case, a private Controller function is probably easiest (it must be private to prevent it being served by web requests):
private getCategories(){
$categoryModel = new CategoryModel();
$ret['categories'] = $categoryModel->orderBy('id', 'ASC')->findAll();
return $ret;
}
Any other function within that same Controller can then get that data by calling on
$this->getCategories().

If anyone faces the same problem, here is the way I manage it. Codeigniter has a nice feature view_cell(). You can create a separate class in Libraries folder (let's call it Menu with method display()) and there return a separate view with your menu. Then in your layout call <?= view_cell('\App\Libraries\Menu::display') ?>. This way there is no need to repeat the same code in many controllers.

Related

Boostrap 4 menu dropdown not showing completely using it in include

I made a menu with bootstrap and php. It works fine but when I use it with an include in another php file the dropdown items are not seen. I don't know if I'm missing a div or should I use something else?
this is my menu.php code:
<body>
<nav class="navbar navbar-expand-sm bg-secondary navbar-dark sticky-top">
<ul class="navbar-nav">
<!-- Brand/logo -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">Administracion</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="ajuste.php">Ajuste Cota</a>
<a class="dropdown-item" href="estaciones.php">Estaciones</a>
<a class="dropdown-item" href="panel.php">Panel de Control</a>
<a class="dropdown-item" href="permisos.php">Permisos</a></div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">Medicion</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="">Puesto 1</a>
<a class="dropdown-item" href="">Puesto 2</a>
<a class="dropdown-item" href="">Puesto 3</a>
</div>
</li>
</ul>
</nav>
</body>
and this is how I use this in another php file
<body>
<?php include("menu.php");?>
<br>
<div class="container">
.
.
.
</body>
I should see the menu as follows:
menu.php:
But when I use menu.php in other file (as include) the menu item does not diplay
menu.php used as include file:
Can someone guide me how to fix this?
Too many <body> tags...you should only have one set (<body>...</body>) in your whole page. You don't need them in the menu file if that's going to be included in another file.

Laravel: Drop down options pulled from database in Master blade template are not showing on other pages

I have created a menu in master blade template in Laravel and have extended that to the other pages. That menu has a dropdown, with several options, that get fetched from the database. While, this works on the master template. It throws the same error on rest of the pages.
How should I fix this?
Here's the error:
"ErrorException
Undefined variable: types (View: /Applications/MAMP/htdocs/bootstrap/laravel-project.app/resources/views/layouts/master.blade.php)"
Here's the web.php code:
Route::get('/master', [MenuController::class, 'show'])
->name('master.show');
Controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\DoshaType;
class MenuController extends Controller
{
public function show(){
$types = DoshaType::all();
return view('layouts/master', compact('types'));
}
}
view code
<!doctype html>
<html>
<head>
</head>
<body>
<div class = "container-fluid">
<div class = "row">
<div class = "col-2 mt-2">
<div class = "logo">
<img src = "./images/logo.png" id= "logo">
</div>
</div>
<div class = "col-10">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary mt-2">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="home">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">A</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Y</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
M
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
#foreach ($types as $type)
<a class="dropdown-item" href="#">{{ $type->dosha }} </a>
#endforeach
</div>
You need to create a global function for that because every page doesn't have $type data. create the function in the helper file.
function types(){
$types = DoshaType::all();
return $type;
}
Now in the master file call this function.
#php $types = types(); #endphp
#foreach ($types as $type)
<a class="dropdown-item" href="#">{{ $type->dosha }} </a>
#endforeach
Another way make this query in your blade file
#php $types = \App\DoshaType::all(); #endphp
#foreach ($types as $type)
<a class="dropdown-item" href="#">{{ $type->dosha }} </a>
#endforeach
you need to do something like this on your master template
<?php
$menu=new dataTable();
$types=$menu->get();
#foreach ($types as $type)
<a class="dropdown-item" href="#">{{ $type->link_title}} </a>
#endforeach
?>

How to add an action in a nav-link in php

I've a project where i need to use the MVC2 architecture. So far so good, but I'm stuck with the navigation bar.
Basically, I'm making a webpage where I have a navigation bar with different options available. When I click in one of the options, I want an "action" to be made, which would be sent to an Action Builder Class which would generate a view with the specific page.
For instance, this is my ActionBuilder class, which is in my Controler folder
<?php
require_once('./controleur/DefaultAction.class.php');
require_once('./controleur/MenuAction.class.php');
require_once('./controleur/LogginAction.class.php');
require_once('./controleur/ContactAction.class.php');
class ActionBuilder{
public static function getAction($nomAction){
switch ($nomAction)
{
case "Menu" :
return new MenuAction();
break;
case "Tarification" :
return new TarificationAction();
break;
case "Contact" :
return new ContactAction();
break;
default :
return new DefaultAction();
}
}
}
?>
Here is my css:
<form method="post" action="">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="navbar-collapse collapse show" id="navbarColor01" style="">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="#" id="APropos">À propos</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" id="Tarification">Tarif</a>
</li>
<li class="nav-item">
<a class="nav-link" href="MenuOffert.php">Menus offert</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" id="Contact">Nous contacter</a>
</li>
<li class="nav-item">
<a class="nav-link" href="Loggin.PHP">Se connecter</a>
</li>
</ul>
</div>
</nav>
After we clicked, for example Contacter in the navigation bar, what I need is to send an action to the controler which would call this ContactAction class:
<?php
require_once('./controleur/Action.interface.php');
class ContactAction implements Action {
public function execute(){
return "Contact";
}
}
which eventually would show the page with the info that I need.
How can I do this?
Also, do I have to set a form in the navigation bar as I did? I'm really not sure for that part.
Thank you.
add your url where you want to display the page to your navigation.
example: À propos
edit the href="#" to href="<?php echo base_url('index.php/add/your/link')"

Yii Widget component cannot find it's view in server

I m new to YII and I have developed a yii app using Eclipse IDE and XAMPP. It perfectly working in windows platform in my local machine. But when I deployed it into server it is not working.
this is my widget controller
"/protected/components/NotificationWidget.php"
<?php
class NotificationWidget extends CWidget {public function run() {
$notifications = Notification::model()->findAllByAttributes(array('Date'=>date('Y-m-d')));;
$this->render('/protected/components/Views/notification.php', array(
'notifications'=>$notifications
));
}}?>
And this is my view
"/protected/components/View"
<li class="dropdown notifications-menu">
<!-- Menu toggle button --> <a href="#" class="dropdown-toggle"
data-toggle="dropdown"> <i class="fa fa-bell-o"></i> <span
class="label label-warning"><?php echo sizeof($notifications);?></span> </a>
<ul class="dropdown-menu">
<li class="header">You have <?php echo sizeof($notifications);?>
notifications</li>
<li>
<!-- Inner Menu: contains the notifications -->
<ul class="menu">
<?php
foreach($notifications as $notification) {
echo "<li>".
"<a href='#'>".
" <i class='fa fa-users text-aqua'></i>". $notification->Description.
"</a></li>";
}
?>
</ul></li>
<li class="footer">View all
</li>
</ul></li>
it gives following error.
NotificationWidget cannot find the view "/protected/components/Views/notification.php".
can any one help on this.
If the server is unix like check for perfect match form lowercase / Uppercasein your path
NotificationWidget cannot find the view "/protected/components/Views/notification.php".
I think should by
NotificationWidget cannot find the view "/protected/components/views/notification.php".

give link to a view in codeigniter

I am trying to give links to different pages in the menu bar. I've created views for the page and corresponding controller is created. Also code for menu bar is written separately in another view. In this view I am trying to give link. I tried giving link with base_url(), but when the page loads links after the link which I given href is not working. I tried:
<a href="<?php echo base_url();?>admember/index">
Here, admember is controller and index is my function.
My code:
header.php
<ul class="nav navbar-nav">
<li class="active">
Dashboard
</li>
<li class="menu-dropdown classic-menu-dropdown ">
<a data-hover="megamenu-dropdown" data-close-others="true" data-toggle="dropdown" href="javascript:;">
Memeber Management <i class="fa fa-angle-down"></i>
</a>
<ul class="dropdown-menu pull-left">
<li class=" dropdown-submenu">
<a href="<?php echo base_url();?>admember/index">
<i class="icon-briefcase"></i>
Add Member </a>
</li>
<li class=" dropdown-submenu">
<a href=":;">
<i class="icon-wallet"></i>
Edit Member </a>
</li>
<li class=" dropdown-submenu">
<a href=":;">
<i class="icon-bar-chart"></i>
Delete Memeber </a>
</li>
</ul>
</li>
</ul>
Controller admember.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class admember extends CI_Controller {
public function index()
{
$this->load->view('addmember');
}
}
Can anyone help me with this. I'm a newbie in codeigniter.
Don't forget to load $this->load->helper('url'); and think problem in base url in Codeginter.
Go to application->config->config.php and change base url example as http://yourbaseurl/

Categories