0% found this document useful (0 votes)
4 views

Laravel Part-3

Uploaded by

waver58650
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Laravel Part-3

Uploaded by

waver58650
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Video-14 (View in Laravel):-

---------------------------------------
1. Views contain the HTML served by your application and separate your
application logic from your presentation logic. Views are stored in the
resources/views directory.

Creating View
resources/views/
about.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Html for views</title>
</head>
<body>
<h1>Hello laravel </h1>
</body>
</html>

Creating Route for View


Syntax:-
1. Route :: get(‘URI’ , function(){
return view(‘view_name’);
});
2. Route ::get(‘URI’ ,function(){
return view(‘folder_name.View_name);
})

Example:-
1. Route :: get(‘about’ , function(){
return view(about);
});

2. Route :: get(‘admin_profile’ , function(){


return view(admin.profile);
});

Note
1. If your route only needs to return a view, you may use the Route::view
method.

Syntax:-
Route :: view(‘URI’,’view_name’);
Route :: view(‘URI’,’folder_name.view_name’);

Example:-
Route ::view(‘about’, ’about’);
Route ::view(admin_profile, ’admin.profile’);

Passing Data from Route to View


1. The data should be an array with key / value pairs.
2. Inside your view, you can then access each value using its corresponding
key.

Example:-
1. Route :: get(‘contact’ , function() {
return view (‘contactme’ , [‘name’ =>’sonam’]);
});

2. Route :: view(‘URI’ , ‘view_file’ , [keys=>’value’]);


Route :: view(‘contact’ , ‘contactme’ ,[‘name’=>’sonam’]);

3. You may use the with method to add individual pieces of data to the view.
Syntax :-
Route :: get(‘URI’ , function(){
return view(‘view_file’)->with(‘key’, ‘value’);
});

Example:-
Route :: get(‘contact’ , function(){
return view(‘contactme’)->with(‘name’, ‘sonam’);
});

Accessing Data which is passed from Route to View


<h1> Hello, {{$Key_Name}} </h1>
<h1> Hello, {{$name}} </h1>
Note:-
Here name is define a key in a web.php file .

Video-15 (Controllers in Laravel):-


------------------------------------------------
1. Controllers can group related request handling logic into a single class.

2. Instead of defining all of your request handling logic as closures in your


route files, you may wish to organise this behaviour using "controller"
classes.

3. Controllers are stored in the App\Http\Controllers\Controller.

4. Controller extends the base controller class included with Laravel.

5. Route ---->(call) Controller -------> (call) views

1. Defining Controller Class


Run Command :- php artisan make:controller controller_name
Example:-
php artisan make:controller AboutController.php

2. Creating Route for Controller Class


Syntax
Route :: get(‘URI’ , [Controller_Name :: class, ’method_name ’]);

Location of controller:- App\Http\Controllers\AboutController.php


Location of Route:- Route\web.php
use App\Http\Controllers\AboutController;
Route :: get(‘about’, [AboutController::class, ’show’]);

3. Getting Parameter in Controller


Location of controller:- App\Http\Controllers\AboutController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
class AboutController extends Controller
{
function show($name)
{
return "Hello Controller".$name;
}

Location of Route:- Route\web.php


<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AboutController;

Route::get('about/{name}',[AboutController::class,'show']);

4. Returning View from Controller Class


Route --------> Controller ---------> View
Location of controller:- App\Http\Controllers\AboutController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AboutController extends Controller


{
function show($name)
{
return view('aboutme');
}
// Inside the Admin folder dashboard view_file call.
function show1($name)
{
return view('admin.dashbaord');
}
}
Location of Route:- Route\web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AboutController;

Route::get('about/{name}',[AboutController::class,'show']);
Route::get('about/{name}',[AboutController::class,'show1']);

Location of view :- resources\view\aboutme.blade.php


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>About Page</title>
</head>
<body>
<h1> About me from Controller</h1>
</body>
</html>

Location of view :- resources\view\admin\dashboard.blade.php


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>Dashboard Page</title>
</head>
<body>
<h1> Dashboard Page</h1>
</body>
</html>

5. Getting URL Parameter in Controller Class and Passing to View


Location of controller:- App\Http\Controllers\AboutController.php
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AboutController extends Controller


{
function show($name)
{
return view('aboutme' ,['name_key'=>$name]);
}
}

Location of Route:- Route\web.php


<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AboutController;

Route::get('about/{name}',[AboutController::class,'show']);

Location of view :- resources\view\aboutme.blade.php


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>About Page</title>
</head>
<body>
<h1>{{$name_key}} Page from Controller</h1>
</body>
</html>

6. Passing Data from Controller to View


Location of controller:- App\Http\Controllers\AboutController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
class AboutController extends Controller
{
function show()
{
$name='Laravel'
return view('aboutme' ,['name_key'=>$name]);
}
}

Location of Route:- Route\web.php


<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AboutController;

Route::get('about',[AboutController::class,'show']);

Location of view :- resources\view\aboutme.blade.php


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>About Page</title>
</head>
<body>
<h1>{{$name_key}} Page from Controller</h1>
</body>
</html>

7. Multiple Methods inside Controller


Location of controller:- App\Http\Controllers\AboutController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AboutController extends Controller


{
function show1()
{
return view('aboutme');
}

function show2()
{
return view('contact');
}

Location of Route:- Route\web.php


<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AboutController;

Route::get('about',[AboutController::class,'show1']);
Route::get('contact',[AboutController::class,'show2']);

Location of view :- resources\view\aboutme.blade.php


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>About Page</title>
</head>
<body>
<h1>About Controller</h1>
</body>
</html>

Location of view :- resources\view\contact.blade.php


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>contact Page</title>
</head>
<body>
<h1>Contact Controller</h1>
</body>
</html>

8. Single Action Controller


If you would like to define a controller that only handles a single action,
you may place a single __invoke method on the controller. To generate this
file run.
Run Command :- php artisan make:controller Controller_Name --invokable
php artisan make:controller ShowController --invokable

Location of controller:- App\Http\Controllers\ShowController.php


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ShowController extends Controller


{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
return view('aboutme');
}
}

Location of Route:- Route\web.php


<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ShowController;

Route::get('about',[AboutController::class]);

Location of view :- resources\view\aboutme.blade.php


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<title>About Page</title>
</head>
<body>
<h1>About Controller</h1>
</body>
</html>

You might also like