Q1. How to cache the configuration in Laravel and why?
Caching the configuration will boost our application speed. We can cache all our configuration files into a single file using config:cache
Artisan command. This will combine all of the configuration options for our application into a single file that can be quickly loaded by the framework. This command php artisan config:cache
typically used as part of our production deployment. The command should not be run during local development as configuration options will frequently need to be changed during the course of our application's development.
Q2. What is HTTP kernels or Console kernels?
When a request is sent to the Laravel application, it first bootstrap the application and creates an instance of the application (see previous questions). Next, the incoming request is sent to either HTTP kernel or console kernel depending on the type of the request. The HTTP kernel is located in app/Http/Kernel.php
. The HTTP kernel extends the Illuminate\Foundation\Http\Kernel
class, which defines an array of bootstrappers that will be run before the request is executed. These bootstrappers configure error handling, configure logging, detect the application environment, and perform other tasks that need to be done before the request is actually handled. Typically, these classes handle internal Laravel configuration that we do not need to worry about. The HTTP kernel also defines a list of HTTP middleware that all requests must pass through before being handled by the application. These middleware handle reading and writing the HTTP session, determining if the application is in maintenance mode, verifying the CSRF token, and more. The method signature for the HTTP kernel's handle method is quite simple: it receives a Request and returns a Response. We can think of the kernel as being a big black box that represents our entire application. The other type of Kernel is Console Kernel which is used when we interact with our application from the command line. If we use artisan commands, or when a scheduled job is processed, or when a queued job is processed, all of these actions go through the Console Kernel.
Q3. What is contextual binding?
Sometimes we may have two classes that utilize the same interface, but we wish to inject different implementations into each class. For example, two controllers may depend on different implementations of the Illuminate\Contracts\Filesystem\Filesystem
contract. Laravel provides a simple, fluent interface for defining this behavior:
Q4. How to extend a binding in Laravel?
The extend
method allows the modification of resolved services. For example, when a service is resolved, we may run additional code to decorate or configure the service. The extend
method accepts a closure, which should return the modified service, as its only argument. The closure receives the service being resolved and the container instance:
Q5. What is the difference between register and boot methods in Service Provider?
In the register
method, we should only bind the service to the containers. We should not register any event listeners, any other piece of functionality. The boot
method is called after all the services are registered. So we have access to all other services that have been registered by the framework. We can type-hint dependencies for our service provider's boot method. The service container will automatically inject any dependencies we need.
Q6. What is Facade in Laravel?
Laravel facades serve as “static proxies” to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods. Laravel ships with many facades which provide access to almost all of Laravel’s features. All of Laravel’s facades are defined in the Illuminate\Support\Facades namespace. So, we can easily access a facade like so:
Facades are easy to use and test and they allow us to use Laravel’s features without remembering long class names that must be injected or configured manually. However, the primary danger of facades is class “scope creep”. Since facades are so easy to use and do not require injection, it can be easy to let our classes continue to grow and use many facades in a single class. Using dependency injection, this potential is mitigated by the visual feedback a large constructor gives us that our class is growing too large. So, when using facades, we need to pay special attention to the size of our class so that its scope of responsibility stays narrow. If our class is getting too large, we need to consider splitting it into multiple smaller classes.
Q7. How to define a route in Laravel with simple closure?
All Laravel routes are defined in our route files, which are located in the routes directory. These files are automatically loaded by our application’s App\Providers\RouteServiceProvider
. The routes/web.php
file defines routes that are for our web interface. These routes are assigned the web
middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php
are stateless and are assigned the api
middleware group. Below is an example of a simple route definition, which will be accessed by our application url followed by /greeting.
Routes defined in the routes/api.php
file are nested within a route group by the RouteServiceProvider
. Within this group, the /api
URI prefix is automatically applied so we do not need to manually apply it to every route in the file. We can modify the prefix and other route group options by modifying our RouteServiceProvider
class.
Q8. What is fallback route in Laravel?
Using the Route::fallback
method, we can define a route that will be executed when no other route matches the incoming request. Typically, unhandled requests will automatically render a "404" page via our application's exception handler. However, since we would typically define the fallback route within our routes/web.php
file, all middleware in the web middleware group will apply to the route. We are free to add additional middleware to this route as needed:
Q9. What is CSRF in Laravel?
Cross-site request forgeries are a type of malicious exploit whereby unauthorized commands are performed on behalf of an authenticated user. Imagine our application has a /user/email route
that accepts a POST
request to change the authenticated user's email address. Most likely, this route expects an email
input field to contain the email address the user would like to begin using. Without CSRF protection, a malicious website could create an HTML form that points to our application's /user/email route
and submits the malicious user's own email address. To prevent this vulnerability, we need to inspect every incoming POST
, PUT
, PATCH
, or DELETE
request for a secret session value that the malicious application is unable to access.
Q10. What is middleware in Laravel?
Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering our application. For example, Laravel includes a middleware that verifies the user of our application is authenticated. If the user is not authenticated, the middleware will redirect the user to our application’s login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application. Additional middleware can be written to perform a variety of tasks besides authentication. For example, a logging middleware might log all incoming requests to our application. There are several middleware included in the Laravel framework, including middleware for authentication and CSRF protection. All of these middleware are located in the app/Http/Middleware
directory.
Q11. How to attach a cookie to a response in Laravel?
We can attach a cookie to an outgoing Illuminate\Http\Response
instance using the cookie method. We should pass the name, value, and the number of minutes the cookie should be considered valid to this method:
If we would like to ensure that a cookie is sent with the outgoing response but we do not yet have an instance of that response, we can use the Cookie
facade to "queue" cookies for attachment to the response when it is sent. The queue
method accepts the arguments needed to create a cookie instance. These cookies will be attached to the outgoing response before it is sent to the browser:
Q12. What is Blade?
Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict us from using plain PHP code in our templates. In fact, all Blade templates are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to our application. Blade template files use the .blade.php
file extension and are typically stored in the resources/views
directory. Blade views may be returned from routes or controllers using the global view
helper. Data may be passed to the Blade view using the view helper's second argument:
We can display the contents of the name
variable like so:
Blade’s {{ }}
echo statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks.
Q13. What is directive in Blade?
Directives are sugar-added functions hiding complex or ugly code behind them. Blade includes lots of built-in directives and also allows us to define custom ones. The built-in ones are more than enough for small projects. But as we find ourselves repeating complex functionality in our code, it is a smell that we need to refactor to custom Blade directives. @if
, @foreach
, @once
, and @include
are some of the built-in directives.
Q14. What is @verbatim
directive?
If we need to display JavaScript variables in a large portion of our template, we can wrap the HTML in the @verbatim
directive so that we do not have to prefix each Blade echo statement with an @
symbol:
Q15. What is @class
directive?
The @class
directive conditionally compiles a CSS class string. The directive accepts an array of classes where the array key contains the class or classes we wish to add, while the value is a Boolean expression. If the array element has a numeric key, it will always be included in the rendered class list:
To read more Interview Questions and answer download our Android App from play store:
https://play.google.com/store/apps/details?id=com.vigowebs.interviewquestions
Our app contains 1400+ Interview Questions and answers with clear code examples from frontend to backend technologies.