diff --git a/laravel.md b/laravel.md index 17bbb0d..43f0986 100644 --- a/laravel.md +++ b/laravel.md @@ -18,6 +18,7 @@ intro: | Getting started {.cols-3} --------------- +[Laravel Docs - Getting Started](https://laravel.com/docs/8.x/installation#your-first-laravel-project) ### Requirements #### PHP @@ -84,11 +85,14 @@ Access application via http://localhost Configuration {.cols-3} --------------- - +[Laravel Docs - Configuration](https://laravel.com/docs/8.x/configuration) ### .env {.cols-2} Retrieve values from `.env` file ```php +env('APP_DEBUG'); + +// with default value env('APP_DEBUG', false); ``` Determine current environment @@ -111,14 +115,14 @@ config(['app.timezone' => 'America/Chicago']); ``` ### Debug Mode -On (local dev): +Turn on (local dev): ```php // .env file APP_ENV=local APP_DEBUG=true // ... ``` -Off (production): +Turn off (production): ```php // .env file APP_ENV=production @@ -131,11 +135,13 @@ Temporarily disable application (503 status code) ```bash php artisan down ``` + #### Bypass Maintenance Mode ```bash php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515" ``` -``` +Visit your application URL `https://example.com/1630542a-246b-4b66-afa1-dd72a4c43515` to set a cookie and bypass the maintenance screen + #### Disable maintenance mode ```bash php artisan up @@ -143,11 +149,12 @@ php artisan up Routing {.cols-4} --------------- +[Laravel Docs - Basic Routing](https://laravel.com/docs/8.x/routing#basic-routing) -Web routes - `routes/web.php` -API routes - `routes/api.php` +Web routes: `routes/web.php` +API routes: `routes/api.php` -### Router Methods +### Router HTTP Methods ```php Route::get($uri, $callback); Route::post($uri, $callback); @@ -167,7 +174,7 @@ Route::any('/', function () { }); ``` -### Basic +### Basic Definition ```php use Illuminate\Support\Facades\Route; @@ -194,7 +201,7 @@ Route::get('/users', function (Request $request) { ``` ### Redirect Routes -HTTP 302 status +HTTP `302` status ```php Route::redirect('/here', '/there'); ``` @@ -202,12 +209,14 @@ Set the status code ```php Route::redirect('/here', '/there', 301); ``` -Permanent 301 redirect +Permanent `301` redirect ```php Route::permanentRedirect('/here', '/there'); ``` ### View Routes +Route only needs to return a view. +First argument: URI, second argument: view name ```php Route::view('/welcome', 'welcome'); @@ -216,6 +225,7 @@ Route::view('/welcome', 'welcome', ['name' => 'Taylor']); ``` ### Route Parameters {.cols-2} +Capture segments of the URI within your route #### Required parameters ```php Route::get('/user/{id}', function ($id) { @@ -262,8 +272,7 @@ Route::get('/user/profile', function () { // })->name('profile'); ``` -See: [Helpers](#helpers) - +See: [Helpers](#helpers-cols-3) ### Route Groups {.cols-2} Share attributes across routes @@ -371,6 +380,8 @@ $action = Route::currentRouteAction(); // string Helpers {.cols-3} --------------- +[Laravel Docs - Helpers](https://laravel.com/docs/8.x/helpers) + ### routes #### Named route ```php @@ -403,6 +414,7 @@ echo route('post.show', ['post' => $post]); // Generating Redirects... return redirect()->route('profile'); ``` +See [Routing](#routing-cols-4) ### URL Generation Generate arbitrary URLs for your application that will @@ -427,6 +439,9 @@ echo url()->full(); echo url()->previous(); ``` ### Named Route URL +```php +$url = route('profile'); +``` See [Named Route](#named-route) ### Error Handling @@ -453,6 +468,8 @@ public function isValid($value) Controllers {.cols-3} --------------- +[Laravel Docs - Controllers](https://laravel.com/docs/8.x/controllers) + ### Basic ```php namespace App\Http\Controllers; @@ -470,18 +487,19 @@ class UserController extends Controller } } ``` -Define a route to this controller method: +Define a route for this controller method: ```php use App\Http\Controllers\UserController; Route::get('/user/{id}', [UserController::class, 'show']); ``` - Requests {.cols-3} --------------- +[Laravel Docs - HTTP Requests](https://laravel.com/docs/8.x/requests) + ### CSRF Protection -Laravel automatically generates a CSRF "token" for each active user session. +Laravel automatically generates a CSRF "token" for each active user session. This token is used to verify that the authenticated user is the person actually making the requests. Get current session's token: @@ -494,7 +512,7 @@ Route::get('/token', function (Request $request) { // ... }); ``` -POST, PUT, PATCH, or DELETE forms should include a hidden CSRF `_token` field +`POST`, `PUT`, `PATCH`, or `DELETE` forms should include a hidden CSRF `_token` field in the form to validate the request. ```html
``` +See [Forms](#forms-cols-3) ### Accessing Request Get an instance of the current request by type-hinting the controller action or route closure ```php // controller action -class UaerController extends Controller +class UserController extends Controller { public function store(Request $request) { @@ -530,7 +549,7 @@ The request's path information ```php $uri = $request->path(); -// http://example.com/foo/bar --> foo/bar +// https://example.com/foo/bar --> foo/bar ``` #### Match path to pattern Verify that the incoming request path matches a given pattern @@ -546,12 +565,14 @@ if ($request->routeIs('admin.*')) { // } ``` -#### URL + +### URL Full URL for the incoming request ```php // URL without the query string $url = $request->url(); +// URL including query string $urlWithQueryString = $request->fullUrl(); // append data to query string @@ -599,16 +620,7 @@ if ($request->accepts(['text/html', 'application/json'])) { // ... } ``` -Determine content type is most preferred by the request -```php -$preferred = $request->prefers(['text/html', 'application/json']); -``` -Shortcut to determine if the incoming request expects a JSON response -```php -if ($request->expectsJson()) { - // ... -} -``` + ### Input Retrieve all the incoming request's input data as an array @@ -624,7 +636,7 @@ $request->collect('users')->each(function ($user) { // ... }); ``` -[See helpers](#helpers) +See [Helpers](#helpers-cols-3) Retrieve user input (also gets values from query string) ```php @@ -661,14 +673,15 @@ $query = $request->query(); #### Boolean Input Values Helpful for checkbox inputs or other booleans. -Return `true` for 1, "1", true, "true", "on", and "yes". +Return `true` for `1`, `"1"`, `true`, `"true"`, `"on"`, and `"yes"`. All other values will return `false` ```php $archived = $request->boolean('archived'); ``` ### Dynamic Properties -Access inputs via properties. If not found as an input, the route parameters will be checked. +Access inputs via properties. +If not found as an input, the route parameters will be checked. ```php $name = $request->name; ``` @@ -711,7 +724,8 @@ Or use the `old()` helper ```php ``` -See: (Helpers)[#helpers] +See: [Helpers](#helpers-cols-3) +See: [Forms](#forms-cols-3) ### Uploaded Files Retrieve uploaded file from request @@ -743,13 +757,14 @@ $path = $request->photo->storeAs('images', 'filename.jpg'); $path = $request->photo->storeAs('images', 'filename.jpg', 's3'); ``` -See More: [File Storage](https://laravel.com/docs/8.x/filesystem) - +See More: [Laravel File Storage](https://laravel.com/docs/8.x/filesystem) Views {.cols-3} --------------- -Presentational logic and HTML. +[Laravel Docs - Views](https://laravel.com/docs/8.x/views) + +Presentational logic and HTML. Create a view by placing a file with the `.blade.php` extension in the `resources/views` directory. ```html @@ -762,14 +777,14 @@ extension in the `resources/views` directory.