VIEWS

What is a route parameter in Laravel?

  • a. A parameter passed in the request headers
  • b. A parameter passed in the request body
  • c. A placeholder value in the route URL
  • d. A parameter passed as a query string

What is a route parameter in Laravel?

  • a. A parameter passed in the request headers
  • b. A parameter passed in the request body

c. A placeholder value in the route URL

  • d. A parameter passed as a query string

What is the primary purpose of Laravel routing constraints?

  • a. To define routes in the web.php file
  • b. To restrict access to certain routes
  • c. To specify rules that route parameters must follow
  • d. To create dynamic routes

What is the primary purpose of Laravel routing constraints?

  • a. To define routes in the web.php file
  • b. To restrict access to certain routes
  • c. To specify rules that route parameters must follow
  • d. To create dynamic routes

Which HTTP method is typically used to create a new resource in Laravel routing?

  • a. GET
  • b. POST
  • c. PUT
  • d. DELETE

Which HTTP method is typically used to create a new resource in Laravel routing?

  • a. GET

b. POST

  • c. PUT
  • d. DELETE

In Laravel routing, what is the purpose of the question mark (?) in route parameters?

  • a. It indicates an optional parameter.
  • b. It signifies a required parameter.
  • c. It is used as a wildcard character.
  • d. It defines a regular expression pattern.

In Laravel routing, what is the purpose of the question mark (?) in route parameters?

a. It indicates an optional parameter.

  • b. It signifies a required parameter.
  • c. It is used as a wildcard character.
  • d. It defines a regular expression pattern.

Which of the following is NOT a valid route parameter constraint in Laravel?

  • a. whereNumber
  • b. whereAlpha
  • c. whereAlphaNumeric
  • d. whereBoolean

Which of the following is NOT a valid route parameter constraint in Laravel?

  • a. whereNumber
  • b. whereAlpha
  • c. whereAlphaNumeric
  • d. whereBoolean

How can you apply global route constraints in Laravel?

  • a. By using the ->constraint() method on specific routes
  • b. By defining constraints in the .env configuration file
  • c. By using the pattern method in the RouteServiceProvider
  • d. By applying middleware to routes

How can you apply global route constraints in Laravel?

  • a. By using the ->constraint() method on specific routes
  • b. By defining constraints in the .env configuration file
  • c. By using the pattern method in the RouteServiceProvider
  • d. By applying middleware to routes

Where are global route constraints typically defined in a Laravel application?

  • a. In the .env configuration file
  • b. In the routes/web.php file
  • c. In the RouteServiceProvider boot method
  • d. In the config/routes.php file

Where are global route constraints typically defined in a Laravel application?

  • a. In the .env configuration file
  • b. In the routes/web.php file
  • c. In the RouteServiceProvider boot method
  • d. In the config/routes.php file

What does the -> arrow (member access operator) primarily signify in Laravel?

  • a. Assignment of values to keys or properties
  • b. Accessing members (attributes or methods) of objects or structures
  • c. Routing to specific controllers
  • d. Applying middleware to routes

What does the -> arrow (member

access operator) primarily signify in Laravel?

  • a. Assignment of values to keys or properties
  • b. Accessing members (attributes or methods) of objects or structures
  • c. Routing to specific controllers
  • d. Applying middleware to routes

What is the purpose of the Route::view method in Laravel routing?

  • a. To create a new route with a view
  • b. To define a named route
  • c. To apply constraints to a route
  • d. To return a view directly for a specified URI

What is the purpose of the Route::view method in Laravel routing?

  • a. To create a new route with a view
  • b. To define a named route
  • c. To apply constraints to a route
  • d. To return a view directly for a specified URI

Introduction

  • Views provide a convenient way to place all of our HTML in separate files.
  • Views separate your controller / application logic from your presentation logic and are stored in the resources/views directory. A simple view might look something like this:

<!– View stored in resources/views/greeting.blade.php –>

<html>

<body>

<h1>Hello, {{ $name }}</h1>

</body>

</html>

Introduction(contd.)

  • Since this view is stored at resources/views/greeting.blade.php, we may return it using the global view helper like so:

Route::get(‘/’, function () {

return view(‘greeting’, [‘name’ => ‘James’]);

});

Creating & Rendering Views

  • You may create a view by placing a file with the .blade.php extension in your application’s resources/views directory.
  • The .blade.php extension informs the framework that the file contains a Blade template.
  • Blade templates contain HTML as well as Blade directives that allow you to easily echo values, create “if” statements, iterate over data, and more.

Nested View Directories

  • Views may also be nested within subdirectories of the resources/views directory.
  • “Dot” notation may be used to reference nested views.
  • For example, if your view is stored at resources/views/admin/profile.blade.php, you may return it from one of your application’s routes / controllers like so:

return view(‘admin.profile’, $data);

Passing Data To Views

  • You may pass an array of data to views to make that data available to the view: return view(‘greetings’, [‘name’ => ‘Victoria’]);
  • When passing information in this manner, the data should be an array with key / value pairs.
  • After providing data to a view, you can then access each value within your view using the data’s keys, such as

<?php echo $name; ?>.

  • Inside Web.php

  • Inside test.blade.php

Passing Data To Views(contd.)

  • As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view.
  • The with method returns an instance of the view object so that you can continue chaining methods before returning the view:

return view(‘greeting’)

->with(‘name’, ‘Victoria’)

->with(‘occupation’, ‘Astronaut’);

One More Shortcut

  • In web.php

  • In test.blade.php

Suppose empty string in passed in city

  • Instead of printing empty string you can also use conditional expression in blade template file and print some default value.

Sharing Data With All Views

  • Occasionally, you may need to share data with all views that are rendered by your application.
  • Typically, you should place calls to the share method within a service provider’s boot method.
  • You are free to add them to the App\Providers\AppServiceProvider class or generate a separate service provider to house them:

Sharing Data With All Views(contd.)

?php

namespace App\Providers;

class AppServiceProvider extends ServiceProvider

{

public function boot()

{

view()->share(‘name’, ‘Baba Yaga’);

}

}

 

[pdf_note link=”https://drive.google.com/file/d/15BASz2l7jcV63Why8XsFCasHxD17Rsfp/view”]