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 resources/views directory.
First view file:
Make a new file with name mywebpage.php in views folder.
- !DOCTYPE html>
- <html>
- <head>
- <title>My first webpage</title>
- </head>
- <body>
- <h1>My Page</h1>
- <p>this is my page</p>
- </body>
- </html>
Now change your routes and controllers as:
- <?php
-
- // change routes.php to like this
- Route::get('request/{id?}','YourController@index');
-
- // in app >> Http >> Controllers >> YourController.php >> YourController
- public function index($id = 12)
- {
- return view('mywebpage');
- }
Now try these urls:
- localhost:8000/request
- http://localhost:8000/request/234
- http://localhost:8000/request/33
NOTE: If view file is inside the directory then dot (.) will be used to navigate to the directory. For example, if 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 a controller. It can be done by sending one more array as a second argument to view() function or by using with() function. For example, we want to pass user id in a template:
- <?php
-
- // no change in routes.php
- // in class YourController
- public function index($id = 12)
- {
- return view('mywebpage',['id' => $id]);
- }
- // in mywebpage.php
- <!DOCTYPE html>
- <html>
- <head>
- <title>My first webpage</title>
- </head>
- <body>
- <h1>My Page</h1>
- <p>this is my page</p>
- <p>my id is <?php echo $id?></p>
- </body>
- </html>
Now try above links again and see the result.
Note:
- 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.
- return view('mywebpage')->with(['id'=>$id]);
Magic Methods:
There is one more way for passing data from controller to view 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).
- public function index($id)
- {
- return view('mywebpage')->withId($id);
- }
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 letters like My_name or myName (but will work for my_name).