Sunday, 24 April 2016

Unknown

Views in Laravel : Create views and use them in applications at EasyLaravel

What are views in laravel :
View separates controllers application logic from presentation logic.
Views are stored in resource/views directory.

Create a view name 'greeting.php' and you can create responsive view that is shown in browser whenever request is made.

Example : Render a view 'greeting.php' when a request '/greeting' is made in the browser .
Step 1:Define a route in Routes.php
" Route::get('/greeting','GreetingController@index'); "

When route request 'greeting' is made GreetingController class index() method is called,in that index() we will reference view using view() method.
Step 2:Create GreetingController using artisana console -
" php artisan make:controller GreetingController "
Step 3 :Add index() method-
 public function index()
{

 }
Step 4: Create view and add html tags -
Create 'greeting.php' file in  resource/views directory.add following content-
<html>
  <body>
     <h1>Welcome</h1>
   </body>
</html>
Step 5:Reference view using view() method in index() method of GreetingController -
 public function index()
{
 return view('greeting');
 }
view() method accepts two parameters -
1.name of view file which is 'greeting'.
2.array of data that should be made available to the view
Step 6:Passing data to view
As we said second argument of view() helper method accepts array of data that we want to pass to view.
Eg: Pass name to view greeting.php 
 public function index()
{
 $name='EasyLaravel';
 return view('greeting',['name'=>$name]);
 }
 Now in view greeting.php file echo $name as follow-
<html>
  <body>
     <h1>Welcome <?php  echo $name?> </h1>
   </body>
</html>


You will get out.
Try it and if any doubts comment below.

Summary:
In View we write Presentation logic.
We reference view file using view() helper functio.

Unknown

About Unknown -

Freelancer,Entrepreneur,Aesthetic bodybuilder.

    Knows :
  • Laravel Framework
  • Ionic Framework + Angular JS
  • Bootstrap
  • CSS
  • Jquery

Subscribe to this Blog via Email :