web tech Flashcards
What are views ?
- in laravel, views are php files that contain html and php code and are used to render a web applications user interface
- it is a method in laravel, that separate the controller logic and domain logic from the presentation logic
- views are located in resource/views folder
how to work with views
- create view file, example, contact.php in resources/views directory
contact.php
<html>
<body>
<h1>Name of the Contact is : </h1>
</body>
</html>
- create route in web.php
web.php
Route::get(‘/contact’, function(){
return view(‘Contact’,[‘name’=>’John’]);
});
In the above code, view() method contains two arguments. The first argument is the name of the file that contains the view, and the second argument is the array passed to the given file. In array, we are passing the name variable to the Contact.php file.
pass data to view using controller
- create controller
In this example, we use the view() method in the Controller class.
Step 1: First, I need to create a controller. Suppose I have created the controller named ‘PostController’, and then add the code given below in a PostController.php file.
public function display(){
return view(‘about’);
}
Step 2: Now, we create the about.php file in which we add the html code.
<html>
<body>
<h1>About Us</h1>
</body>
</html>
Step 3: Last step is to add the route in web.php file.
Route::get(‘/post’,’PostController@display’);
Step 4: Enter the URL http://localhost/laravelproject/public/post to the web browser.
how nesting views work
- Views can also be nested within the sub-directory resources/views directory.
Let’s understand the nested views through an example.
Suppose we want to know the admin details. The view of the admin details is available at the resources/views/admin/details.blade.php directory.
Step 1: First, we create details.blade.php file in the admin folder, and the code of the details.blade.php file is given below:
<html>
<body>
<h1>Admin Details</h1>
</body>
</html>
Step 2: Now, we need to add the display() function in PostController.php file which is returning the view of the ‘admin.details’.
public function display(){
return view(‘admin.details’);
}
Step 3: Lastly, we will add the route in a web.php file.
Route::get(‘/details’, ‘PostController@display’);
Step 4: To see the output, enter the url ‘http://localhost/laravelproject/public/details’ to the web browser.
How passing data to views work?
- different ways
- we can directly pass data through routes or controller
Some ways
- using view()
- using with()
- using compact()
- using controller class
How to pass data using view()
how to pass data using with() method
how to pass data using compact()
how to pass data using controller class
How to pass data to all routes
https://youtu.be/XWB3z5ekIN8