views आपके Laravel Application का view part रखते हैं , या कह सकते हैं कि आपके Application का HTML part views ही contain करते हैं। view files को resources/views directory में रखते हैं।


view files को Laravel Application में हम .blade.php extension के साथ save करते हैं।

Making A Simple View

/*Step 1 : add this route in routes.web.php*/
Route::get('/hello', function(){
   return view('hello');
});

/*Step 2 : create file resources/views/hello.blade.php*/
<!DOCTYPE html>
<html>
	<head>
		<title>Hello ! Laravel</title>
	</head>
	<body>
		<h4>Hello ! Laravel</h4>
	</body>
</html>

/*that's it.
*Now hit : http://www.yourdomain.com/hello
*/

हालाँकि अभी view closure function से return किया गया है , आप चाहे तो same statement को Controller Method में लिखकर view return करा सकते हैं।

File : app/Http/Controllers/HomeController.php

Copy Fullscreen Close Fullscreen
<?php 
 namespace App\Http\Controllers; 
 use Illuminate\Http\Request; 
 class HomeController extends Controller
 {
      /**
      * @param  Request  $request : contains all info about current request
      */
     public function index(Request $request)
     {
         return view('hello');
     }
 }
?>

In routes/web.php

Route::get('/hello', 'HomeController@index');

Laravel Passing Data To Views

view में हम दो तरह से data pass कर सकते हैं -

  1. Using array
  2. Using compact() method

दोनों में से कोई भी तरीका use कर सकते हैं।
For Example :

/*Using array*/
return view('hello', ['name'=> 'Rahul Kumar']);

/*Using compact() method*/
$name = 'Rahul Kumar';
return view('hello', compact('name'));

कुछ इस तरह से Controller से view पर data pass करते हैं। अब अपने view file पर key(name) से उस value को access कर सकते हैं।

File : resources/views/hello.blade.php

Copy Fullscreen
<!DOCTYPE html>
<html>
	<head>
		<title>Hello ! Laravel</title>
	</head>
	<body>
		<h4>Hello ! {{$name}}</h4>
	</body>
</html>

Important
Laravel हमें double curly braces {{}} functionality provide करता है , जिससे view file पर हम double curly braces {{5+9}} के अंदर कोई भी valid PHP expression लिख सकते हैं। यह <?php echo(5+9) ?> में automatically convert हो जाता है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers