Laravel Framework

109 flashcards by MentisLibrary

Laravel end to end for PHP developers: routing and middleware, controllers, Eloquent relationships and migrations, Blade templates and components, validation, authorization, queued jobs and feature testing.

What is Laravel?
A PHP web framework built on Symfony components that ships with routing, the Eloquent ORM, the Blade template engine, queues, authorization and a command line tool called Artisan.
What does php artisan do?
It is Laravel's command line entry point: it generates classes, runs migrations, clears caches, starts queue workers and opens a REPL.
Where does application configuration live?
In PHP files under config/ that return arrays. Each value normally reads an environment variable from .env through the env() helper.
What is APP_KEY used for?
A random 32-byte key stored in .env that Laravel uses to encrypt cookies, session payloads and anything passed through the encrypter.
What is Laravel's service container?
The dependency injection container. It resolves a class and its constructor dependencies automatically, and holds bindings that map an interface to a concrete implementation.
What is a service provider?
A class with a register() method for container bindings and a boot() method for work that runs once every provider is registered, such as view composers, macros and policies.
What is a facade?
A static-looking proxy that resolves an object from the service container and forwards the call to it, so Cache::get() really calls get() on the resolved cache instance.
What does php artisan storage:link do?
Creates a symbolic link from public/storage to storage/app/public so files written to the public disk can be served over HTTP.
Which caches should a production deploy rebuild?
Config, routes, events and views. config:cache, route:cache, event:cache and view:cache compile each of them into a single fast file.
What belongs in app/, routes/, resources/ and public/?
app/ holds application classes such as models, controllers and jobs; routes/ the route files; resources/ Blade views and uncompiled assets; public/ the document root and built assets.
routes/web.php vs routes/api.php
Both register routes, but web routes get session state, cookies and CSRF protection, while API routes are stateless and served under an /api prefix.
How do you declare a route parameter?
Wrap the URL segment in braces. The captured values are passed to the handler in the order they appear in the path.
What is a named route?
A route labelled with ->name(), so URLs are generated from the name instead of being written out by hand.
What is route model binding?
Type-hinting an Eloquent model in the handler makes Laravel look the record up by the route parameter and inject it, returning a 404 when nothing matches.
How do you bind a route to a column other than the primary key?
Name the column in the route as {post:slug}, or override getRouteKeyName() on the model so every binding uses it.
What does a route group do?
Applies shared attributes to every route inside it: middleware, a URL prefix, a route-name prefix, or a controller.
What does Route::resource register?
The seven conventional CRUD routes for a controller in one call: index, create, store, show, edit, update and destroy.
How do you inspect every registered route?
php artisan route:list prints each route with its method, URI, name, action and middleware, in registration order.
How is middleware attached to a route?
With ->middleware() on a single route or on a group. Middleware runs before the handler and may short-circuit the request.
How do you rate limit a route?
Apply the throttle middleware with a maximum and a window in minutes, or define a named limiter and reference it by name.
How do you create a controller?
With the make:controller generator, optionally asking for the resource methods or an API variant.
What is a single action controller?
A controller with one public method named __invoke, referenced in a route by class name alone.
How does a controller read request input?
Type-hint Illuminate\Http\Request and use input(), query(), boolean() or file(); missing keys return null or a default.
How do you return JSON from a controller?
Return an array, a model or a collection and Laravel serializes it, or call response()->json($payload, 201) when you need a specific status.
What is an Eloquent API resource?
A class that transforms a model into an array for a JSON response, giving one place to decide the shape of the payload.
What should a controller return after a successful form post?
A redirect, not a rendered page, so a browser refresh does not resubmit the form. Flash a message onto the session on the way.
How do you send the user back with their input?
Return back() and chain withInput() and withErrors() so the form can be redrawn with the old values.
How does a controller receive its dependencies?
Type-hint them in the constructor or in the action method; the service container resolves and passes them automatically.
How do you handle a file upload?
Validate it, then call store() on the uploaded file with a directory and a disk; Laravel generates a unique name and returns the stored path.
How do you send a 404 from inside a controller?
Call abort(404), or use findOrFail() and let the ModelNotFoundException be converted for you.