Laravel Interview Questions For Mediators 2024

Other Blogs

Blogs ❯❯ Laravel

Image could not load

Laravel Interview Questions

Laravel Interview Questions For Mediators

Laravel में intermediate level के developers को advanced concepts और best practices की समझ होना जरूरी है। अगर आप Laravel mediator हैं और interview की तैयारी कर रहे हैं, तो यह blog आपको कुछ ऐसे questions और उनके answers provide करेगा जो आपको help कर सकते हैं।

1. What is service container and how is it used in Laravel ?

Service Container एक dependency injection container है जो classes और dependencies को resolve करने में मदद करता है। इससे hum classes के objects को manage कर सकते हैं और code को loosely coupled और manageable बना सकते हैं।

// Binding an interface to a class. $this->app->bind('App\Contracts\PaymentInterface', 'App\Services\StripePayment'); // Resolving a class. $paymentService = app('App\Contracts\PaymentInterface');

2. What is a service provider ?

Service Providers Laravel applications के bootstrapping process का essential part हैं। यह services को register करते हैं और application को configure करते हैं। Laravel में हर एक major component का अपना service provider होता है।

Example : app/Providers/AppServiceProvider.php

public function register() { // Register services } public function boot() { // Bootstrap services }

3. What are facades and when are they done?

Facades static interfaces provide करते हैं Laravel application के internal classes के लिए , यह code को clean और readable बनाते हैं और behind थे scenes service container का use करते हैं।

use Illuminate\Support\Facades\Cache; // Setting a value in cache. Cache::put('key', 'value', $minutes); // Retrieving a value from cache. $value = Cache::get('key');

4. What are events and listeners in Laravel?

Events और Listeners pattern का use asynchronous processing और loose coupling को achieve करने के लिए होता है। Events को trigger किया जा सकता है और listeners उन events पर respond कर सकते हैं।

  • Event class : php artisan make:event OrderShipped

  • Listener class : php artisan make:listener SendShipmentNotification

// Firing an event. event(new OrderShipped($order)); // Handling an event. public function handle(OrderShipped $event) { // Access the order using $event->order }

5. What is Queues and how is it implemented in Laravel ?

Queues background tasks को handle करने के लिए use की जाती हैं, जैसे emails भेजना या reports generate करना। Laravel में queues को configure और process करने के लिए एक unified API provide किया गया है।

Steps to implement :

  • Configure queue driver : In .env file, set QUEUE_CONNECTION=database .

  • Create a job : php artisan make:job SendEmailJob

Dispatch a job

use App\Jobs\SendEmailJob; // Dispatch a job to the queue SendEmailJob::dispatch($details);

Run the queue worker : php artisan queue:work

6. How is middleware created and implemented ?

Middleware एक HTTP request को process करने के लिए एक filtering mechanism provide करता है। यह requests को modify या inspect करने की सुविधा देता है।

For example :

1. Create a middleware: php artisan make:middleware CheckRole

public function handle($request, Closure $next, $role) { if (! $request->user()->hasRole($role)) { return redirect('home'); } return $next($request); }

2. Register middleware: In app/Http/Kernel.php, register the middleware

protected $routeMiddleware = [ 'role' => \App\Http\Middleware\CheckRole::class, ];

3. Use middleware in routes :

Route::get('admin', function () { // })->middleware('role:admin');

7. What is the importance of dependency injection in Laravel ?

Dependency Injection एक design pattern है जिसमें objects को उनकी dependencies externally provide की जाती हैं, instead of creating them within थे class. Laravel के service container के साथ, यह बहुत efficiently manage किया जाता है।

For example :

class OrderController extends Controller { protected $orderService; public function __construct(OrderService $orderService) { $this->orderService = $orderService; } public function store(Request $request) { $this->orderService->create($request->all()); } }

8. What are Laravel Gates and Policies?

Gates और Policies authorization को manage करने के लिए use की जाती हैं। Gates एक closure based approach है जबकि Policies classes हैं जो एक particular model के authorizations को define करती हैं।

Gate::define('update-post', function ($user, $post) { return $user->id === $post->user_id; });

Laravel Policies Example

1. Create a policy: php artisan make:policy PostPolicy --model=Post

2. Define policy methods :

public function update(User $user, Post $post) { return $user->id === $post->user_id; }

3. Register policy: In AuthServiceProvider :

protected $policies = [ 'App\Models\Post' => 'App\Policies\PostPolicy', ];

4. Use policy

if (Gate::allows('update-post', $post)) { // The user can update the post }

9. What is Laravel Scout ?

Laravel Scout एक full-text search package है जो Eloquent models के लिए driver based search functionality provide करता है। यह external search engines, जैसे Algolia, को integrate करके fast and powerful search capabilities add करता है।

Steps to use Laravel Scout :

1. Install Scout package

composer require laravel/scout

2. Publish Scout configuration

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

3. Add Searchable trait to the model

use Laravel\Scout\Searchable; class Post extends Model { use Searchable; }

4. Perform a search query

$posts = App\Models\Post::search('Laravel')->get();

10. What is Laravel Tkinter ?

Laravel Tinker एक REPL (Read-Eval-Print Loop) tool है जो interactively Laravel applications के साथ interact करने की facility provide करता है। यह real-time testing और debugging के लिए बहुत useful है।

php artisan tinker >>> $users = App\Models\User::all(); >>> $user = App\Models\User::find(1);

Conclusion

यह कुछ common Laravel interview questions हैं जो mediators को interview के लिए prepare करने में मदद कर सकते हैं। इन concepts को समझने और practice करने से आप Laravel के advanced features और best practices को अच्छी तरह से grasp कर सकते हैं। Interview के लिए अच्छी तैयारी करने के लिए latest Laravel documentation और community resources को refer करना important होगा।

Happy coding!

Recent Blogs

Loading ...

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    0 Comment(s) found !