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
Related
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.
I'm making a "teacher's" app, and I want to make a log-in page which changes depending if there's registered users in the database or not.
I want to make a redirection button to a create user page if there aren't auth users in database, and to make a select user view if the database have one or more users.
The problem is that I don't know how to exactly do this, 'cause the view always shows me the first statement (what I've got in the if), also if in the database are registered users. Can anyone help me with this please?
This is the blade file:
#if (empty(Auth::user()->id))
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
Here you have the controller index method:
public function index()
{
$users = User::all();
return view('/', compact('users'));
}
And finally here you have the page:
The following code is the sample for it, kindly replace code accordingly
#if(!$user)
//show button
#else
//dont show button
#endif
I think your question is you want to check if there is user in database.
So no need to check if the user authenticated but to check if there is user on the database.
In your controller
public function index() {
return view('/', ['users' => User::all()]);
}
and in your blade file
#if(!$users)
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
This function will get the current authenticated user: Auth::user(). I guess what you are trying to achieve is #if(empty($users)) where $users is the variable you are passing on controller.
If you want to verify if the user that accessed to that view is authenticated you can simply use #auth and #guest.
Also i would suggest you to change your button to an <a> tag and your href would be <a href="{{ route('route.name') }}" where route.name would be defined in your routes file.
in your controller:
you can create a folder inside views called users and then the index.blade.php (views/users/index.blade.php)
public function index()
{
$users = Users::all();
return view('users.index')->with('users', $users);
}
in your view:
#if(count($users) < 1)
...
#else
...
#endif
count is validating if the $users array length is less then 1 (in other words if the array is empty).
Alternative you can you isEmpty()
#if($users->isEmpty())
...
#else
...
#endif
`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
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';
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