How to load view file content in Codeigniter 4? - php

I am working in Codeigniter 4 and trying to call $this->renderSection from view. This is my dir structure.
controller
--AZ
-- Dashboard.php
views
--AZ
-- layout
--- template.php
--include
--- dashboard-layout.php
dashboard.php
first i am trying to call AZ directory view.This is my controller file Dashboard.php.
<?php
namespace App\Controllers\AZ;
use App\Controllers\BaseController;
class Dashboard extends BaseController
{
public function __construct()
{
$session = session();
if (!$session->get('admin_id'))
{
redirect('login');
}
}
public function index()
{
$data['maincontent']='AZ/dashboard';
return view('AZ/layout/template',$data);
}
}
This is my template.php file in layout directory.
<?= $this->extend('AZ/template/dashboard-layout') ?>
<?= $this->section($maincontent) ?>
<?= $this->endSection() ?>
It's showing header but not showing content from dashboard.php file in view.
dashboard.php in view/AZ dirctory.
<div id="content">
<div id="content-header">
<div id="breadcrumb"> <i class="icon-home"></i> Home</div>
</div>
</div>
In dashboard-layout.php there is my header then i am trying to call view by using
$this->renderSection($maincontent);
and then footer. I am using dashboard-laayout.php in layout.php file. I have added code above of this file.

Related

Show.blade.php is not displaying the content from the database, only the layout

`i am having a problem with my show.blade.php template, everything works fine but when I click on a post in the index page it directs me to the /post/1 page without showing the post content only the extended layout. please help
Web.php
Route:: resource('best-practices' , 'BestpracticesController');
*bestpracticescontroller.php
public function index()
{
$bestpractices = Bestpractices::all();
return view('bp.index',compact('bestpractices'));
}
public function show(Bestpractices $bestpractices)
{
return view('bp.show',compact('bestpractices'));
}
bp.show view template
#extends('layouts.front')
#section('content')
<div class="blog-details pt-95 pb-100">
<div class="container">
<div class="row">
<div class="col-12">
<div class="blog-details-info">
<div class="blog-meta">
<ul>
<li>{{$bestpractices->Date}}</li>
</ul>
</div>
<h3>{{$bestpractices->title}} </h3>
<img src="{{asset('storage/'.$bestpractices->cover_img)}}" alt="">
<div class="blog-feature">
{{$bestpractices->body}}
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
Thats because when you register routes via
Route::resource('best-parctices', BestparcticeController');
//Generated show route is equivalent to
Route::get(
'/best-practices/{best_practice}',
[BestpracticeController::class, 'show']
);
//route parameter is best_practice
Hence to achieve implicit route model binding the route parameter name must match the parameter name in the controller method
public function show(Bestpractices $bestpractices)
{
//here $bestpractices will be an int and not an object with
//model record as implicit route model binding doesn't work
return view('bp.show',compact('bestpractices'));
}
public function show(Bestpractices $best_practice)
{
//here implicit route model binding works so $best_practices is an object
//with model record
return view('bp.show',['bestpractices' => $best_practices]);
}
Or if you don't want to change the method parameter name in the controller methods then you need to override the route parameter name in the Route:resource() call when you define routes
Route::resource('best-practices', BestpracticesController::class)
->parameters([
'best-practices' => 'bestpractices'
]);
Laravel docs: https://laravel.com/docs/8.x/controllers#restful-naming-resource-route-parameters

How can I call a page when i clicked the button in codeiginiter?

I am not calling a page in codeiginiter when i click button..
i am new in this so please HELP....
<i class="fa fa-check-square-o nav_icon"></i>Forms<span class="fa arrow"></span>
<ul class="nav nav-second-level collapse">
<li>
Inputs
</li>
<li>
Form Validation
</li>
</ul>
on theme implement in codeiginiter i can't call the page
it's is show the page was not found but i given correct path on it.
First you have to understand MVC Architecture..
You just have to call Controller/Method where you call your view.
example:
Controller
<?php
class Mycontroller extends CI_Controller{
public function orders(){
$this->load->view('orderpage');
}
}
?>
view - Orderpage.php
<body>
<h3>orders list</h3>
</body>
your just have to add anchor tag like
Go to orders
Edit config/config.php file
$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/codeigniter/';
in view
Inputs
you need to setup routs for View
go to
\application\config\routes.php
and add a route at the end like this
$route['ViewName'] = 'ViewPath';
My views are presented in
application\views\user
so i will add
$route['confirm'] = 'user/confirm';

Laravel Blade view not working

I am using Laravel 5.3.28 version. I have created main view(main.blade.php) and trying to extract the details from different child views(navbar.blade.php) which is in view/pages directory but its not working.
Route file in project/routes/web.php
Route::get('/', function () {
return view('main');
});
Main view in resources/views/main.blade.php(this file will receive the data from navbar.blade.php)
<div id="column">Test1</div>
<div id="container">
#yield('content')<!--This will get the content from navbar.blade.php-->
</div>
child view in resources/views/pages/navbar.blade.php
#extends('main')
#section('content')
<div id="row">
Test2
</div>
#endsection
I am able to see output as Test1 but not Test2. Why it this happening?
To see navbar content you should return navbar view:
Route::get('navbar', function () {
return view('navbar');
});
But if you're planning to include navbar in many views, you should use #include() instead of using section(), extends() and yield():
#include(`navbar`)
you should call view('navbar') instead of view('main') because navbar is a child of main view
if you call parent child content will not be seen there for call view('navbar')
For seeing the output Test2 you have to return navbar view
Route::get('/', function () {
return view('navbar');
});
#yield is a place when you will extends main view then you can put something inside this content.
I am explaining below by an example using your view file
you can simply put this in navbar.blade.php. it doesn't needs to be extended.
<div id="row">
Test2
</div>
In your main.blade.php you can put this
<div id="column">Test1</div>
<div id="container">
#include('navbar')
#yield('content')
</div>
The suppose you have decided to create a third view which will extends main view with navbar view. suppose your created a view named home.blade.php and add this to your home.blade.php view
#extends('main')
#section('content')
Welcome Home !!!
#endsection
Then if you calls create your route like below
Route::get('/home', function () {
return view('home');
});
It will render like below
<div id="column">Test1</div>
<div id="container">
<div id="row">
Test2
</div>
Welcome Home !!!
</div>
Hope this will clear your concept about view rendering

Laravel not loading view. Error in a storage\framework\view file

I'm unable to access my orders_index view from my OrdersController.
orders_index.blade.php
location: \resources\views\orders
#extends 'app'
#section('heading')
Orders Index
#endsection
#section('content')
<section>
content section
</section>
#endsection
OrdersController
location: app\Http\Controllers
public function index(){
return view('orders.orders_index');
}
routes.php
Route::get('orders', 'OrdersController#index');
file in which error is indicated: f2e7d6f3f58a0e30e17a0c631c812b28
'/app'
<?php $__env->startSection('heading'); ?>
Orders Index
<?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>
<section>
content section
</section>
<?php $__env->stopSection(); ?>
<?php echo $__env->make(, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
I think it has something to do with namespacing, which I have not quite figured out yet. I haven't seen any information online yet about namespacing in the view, so I do not know if I am to use namespace in the views.
It should be #extends('app') not #extends 'app'. For Laravel what you did now was like writing: #extends() 'app' So it tries to call the function make with an empty first parameter (because you didn't pass anything to #extends) and hence you get that syntax error.

coneigniter: linking controllers

I would like to link a controller to another page.
I have an index page that contains the login and now I need to link the controller and the login link:
<div class="toplogin">
<p>SIGN IN <span class="separator">|</span> REGISTER</p>
</div>
<div class="header_phone">
<p>CALL US NOW: <strong>1-800-531-453</strong></p>
</div>
<div class="clear"></div>
</div>
You need to link SIGN IN where "controller_name" is the name of your controller file and "login" is the name of the method.
In your controller_name.php file:
public function login(){
//do login stuff
$this->load->view('login');
}
Usually in codeignitor, we link by calling the controller function eg, if you have a controller register in which you have this function getdata(), then we link it by calling REGISTER

Categories