View in Laravel 5

Views are the HTML content served by the server. These are served by returning from controllers. As discussed in last tutorial, views are .php or .blade.php files which are stored in folder resources/views.

First view file:

Make a new file with name mywebpage.php in views folder.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>My first webpage</title>
  5. </head>
  6. <body>
  7. <h1>My Page</h1>
  8. <p>this is my page</p>
  9. </body>
  10. </html>

Now change your routes and controllers as:

  1. // change routes.php to like this
  2. Route::get('request/{id?}','YourController@index');
  3. // in app >> Http >> Controllers >> YourController.php >> YourController
  4. public function index($id = 12)
  5. {
  6. return view('mywebpage');
  7. }

Now try these urls:

  • localhost:8000/request
  • http://localhost:8000/request/234
  • http://localhost:8000/request/33

NOTE: If view file is inside the folder then dot (.) will be used to navigate to folder. For example, 503.blade.php is in errors folder then we will use view('errors.503') for rendering this file.

Sending variables in template files:

Template files may require some additional information. This information is sent through controller. It can be done by sending one more array as second argument to view() function or by using with() function. For example, we want to pass user id in template:

  1. // no change in routes.php
  2. // in class YourController
  3. public function index($id = 12)
  4. {
  5. return view('mywebpage',['id' => $id]);
  6. }
  7. // in mywebpage.php
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <title>My first webpage</title>
  12. </head>
  13. <body>
  14. <h1>My Page</h1>
  15. <p>this is my page</p>
  16. <p>my id is <?php echo $id?></p>
  17. </body>
  18. </html>

Now try above links again and see the result.

Note:

  1. In array (second argument of view()), key is the variable name which is required in template and value is the variable in controller. In other words, we are mapping a template required variable to a variable which is accessible in controller.

WIth function:

Variable/ data can be send using with() function. with() function also takes array and do the same work.

  1. return view('mywebpage')->with(['id'=>$id]);

Magic Methods:

There is one more way for passing data from controller to views file called Magic Methods. This way is used only when we have to pass single variable. A function is generated dynamically with prefix "with" and followed by capitalized variable name. It takes only one argument, value of variable. In our case, this function is withId($id).

  1. public function index($id)
  2. {
  3. return view('mywebpage')->withId($id);
  4. }

For general use function with+(required variable name with first capital letter)(). For example:

Variable Name Magic Method
name withName()
my_name withMy_name()
my_name_8 withMy_name_8

NOTE: Magic methods are only work for variable with name in snack case (only lowercase letters, number and _). It will not work for the variables which contains capital letter like My_name or myName (but will work for my_name).

More Tutorials on laravel in this series:



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Laravel is a MVC framework. Models are the real world entities in data form. Views are the response content to the vi...
Laravel framework architecture
Routes map urls with the controllers. In this section we will read about how route maps urls with controllers and how...
Basics routing and controllers in laravel 5
Views are the HTML content served by the server. These are served by returning from controllers. Views are .php or .b...
View in Laravel 5

Login or Sign up to leave comment.