<<

APPENDIX

More New Features in Laravel 5.8

Laravel 5.8 has many improvements and updates. Some of them seem to be quite interesting and will impact the course of future development. In this appendix, you’ll take a quick look at some of them. One of the most interesting changes is the “dump server” feature. It was first incorporated in Laravel 5.7 and then extended in Laravel 5.8.

What Is the Dump Server Feature?

The dump server feature allows you to start a “dump server” to collect dump information about the internal processes. Let’s start a new version of Laravel 5.8 installed locally. Then open the . json file and take a look at this part:

"require-dev": { "beyondcode/laravel-dump-server": "^1.0", "filp/whoops": "^2.0", "fzaninotto/faker": "^1.4", "mockery/mockery": "^1.0", "nunomaduro/collision": "^2.0", "/phpunit": "^7.5" },

You’ll see beyondcode/laravel-dump-server": "^1.0" already in place.

399 © Sanjib Sinha 2019 S. Sinha, Beginning Laravel, https://doi.org/10.1007/978-1-4842-4991-8 Appendix More New Features in Laravel 5.8

Next, open the routes/web. file and add this line:

Route::get('/', function () {

dump("Hello Laravel 5.8");

return view('welcome'); });

This gives you the output shown in Figure A-1.

Figure A-1. Dump server output in the browser

Next, in the terminal, so ahead and pass this command:

$ php artisan dump-server It gives us this output: ss@ss-H81M-S1:~$ cd code/laravel58/ ss@ss-H81M-S1:~/code/laravel58$ php artisan dump-server

400 Appendix More New Features in Laravel 5.8

Laravel Var Dump Server ======

[OK] Server listening on tcp://127.0.0.1:9912

// Quit the server with CONTROL-C.

GET http://localhost:8000/ ------date Wed, 03 Apr 2019 00:33:20 +0000 controller "Closure" source web.php on line 16 file routes/web.php ------"Hello Laravel 5.8"

Figure A-2 shows the output in the terminal. It is interesting to take note that in the browser, at the same time, the page just vanishes.

Figure A-2. Dump server output in the terminal 401 Appendix More New Features in Laravel 5.8

Let’s dump all the users the same way. Remember, I have seeded the users table with fake data, and currently there are 21 users in the . Go ahead and change your routes/web.php file in this way: use App\User;

Route::get('/', function () {

$users = User::all();

dump($users);

return view('welcome'); });

Open your browser, and you will see something like Figure A-3.

Figure A-3. The browser is filled with the dumped user data

However, you can manage this output by running the dump-server artisan command in the terminal. You will then get the clean output in the browser (Figure A-4).

402 Appendix More New Features in Laravel 5.8

Figure A-4. Clear browser without dumped user data

Instead, all the user data is being displayed in the terminal, as shown in Figure A-5.

403 Appendix More New Features in Laravel 5.8

Figure A-5. Dumped user data in the terminal

Now change the routes/web.php code to the following and run the dump-server command again: use App\User;

Route::get('/', function () {

$users = User::all()->toArray();

dump($users);

return view('welcome'); });

404 Appendix More New Features in Laravel 5.8

The output is dumped in the terminal like in Figure A-6. ss@ss-H81M-S1:~/code/laravel58$ php artisan dump-server

Laravel Var Dump Server ======[OK] Server listening on tcp://127.0.0.1:9912 // Quit the server with CONTROL-C. GET http://localhost:8000/ ------date Wed, 03 Apr 2019 00:56:02 +0000 controller "Closure" source web.php on line 19 file routes/web.php ------array:22 [ 0 => array:10 [ "id" => 1 "country_id" => 5 "role_id" => 4 "name" => "Sarina Becker MD" "email" => "[email protected]" "email_verified_at" => null "created_at" => "2018-11-17 03:46:58" "updated_at" => "2018-11-28 03:15:55" "admin" => 0 "mod" => 0 ] 1 => array:10 [ "id" => 2 "country_id" => 7 "role_id" => null "name" => "Ariel Hand" "email" => "[email protected]" "email_verified_at" => null "created_at" => "2018-11-17 03:46:59" 405 Appendix More New Features in Laravel 5.8

"updated_at" => "2018-11-17 03:46:59" "admin" => 0 "mod" => 0 ] 2 => array:10 [ "id" => 3 "country_id" => null "role_id" => null "name" => "Carissa Raynor" "email" => "[email protected]" "email_verified_at" => null "created_at" => "2018-11-17 03:46:59" "updated_at" => "2018-11-17 03:46:59" "admin" => 0 "mod" => 0 ] 3 => array:10 [ "id" => 4 "country_id" => null "role_id" => null "name" => "Jude Johnston" "email" => "[email protected]" "email_verified_at" => null "created_at" => "2018-11-17 03:46:59" "updated_at" => "2018-11-17 03:46:59" "admin" => 0 "mod" => 0 ] 4 => array:10 [ "id" => 5 "country_id" => null "role_id" => null "name" => "Sarai Beier" "email" => "[email protected]" "email_verified_at" => null "created_at" => "2018-11-17 03:46:59" 406 Appendix More New Features in Laravel 5.8

"updated_at" => "2018-11-17 03:46:59" "admin" => 0 "mod" => 0 ] 5 => array:10 [ "id" => 6 "country_id" => null "role_id" => null "name" => "Ms. Freda Kemmer" "email" => "[email protected]" "email_verified_at" => null "created_at" => "2018-11-17 03:46:59" "updated_at" => "2018-11-17 03:46:59" "admin" => 0 "mod" => 0 ]

I have omitted some output here for brevity.

Improved artisan Command

Laravel 5.8 now has improved artisan commands. Before Laravel 5.8, you could not run two servers in parallel on different ports. For example, suppose you are running your application in Laravel 5.7 on port 8000. At the same time, you want to run your Laravel 5.8 application on another port. With previous versions, Laravel ran the servers on one port, 8000. Laravel 5.8 has the ability to use another port. Let’s see an example. I have a news application in my code/news directory. It runs on Laravel 5.7. I start it, and along with it, I start a new Laravel 5.8 application in the code/ laravel58 directory. The first artisan serve command has normal output like this:

//output of Laravel 5.7 php artisan serve command cdss@ss-H81M-S1:~$ cd code/news/ ss@ss-H81M-S1:~/code/news$ php artisan serve Laravel development server started:

407 Appendix More New Features in Laravel 5.8

[Wed Apr 3 08:22:26 2019] 127.0.0.1:40984 [200]: /css/webmag/css/ bootstrap.min.css [Wed Apr 3 08:22:26 2019] 127.0.0.1:40986 [200]: /css/webmag/css/font-­ awesome.min.css [Wed Apr 3 08:22:26 2019] 127.0.0.1:40988 [200]: /css/webmag/css/style.css [Wed Apr 3 08:22:26 2019] 127.0.0.1:40990 [200]: /css/webmag/js/. min.js [Wed Apr 3 08:22:26 2019] 127.0.0.1:40992 [200]: /css/webmag/js/bootstrap. min.js [Wed Apr 3 08:22:26 2019] 127.0.0.1:40994 [200]: /css/webmag/js/main.js [Wed Apr 3 08:22:26 2019] 127.0.0.1:40996 [200]: /css/webmag/img/logo.png [Wed Apr 3 08:22:27 2019] 127.0.0.1:41004 [200]: /images/entertainment/ rowan-chestnut-175871- unsplash.jpg [Wed Apr 3 08:22:27 2019] 127.0.0.1:41006 [200]: /css/webmag/img/ widget-1.jpg (Code is incomplete for brevity)

Next, I run the new Laravel 5.8 application in code/laravel58. Take a look at the output in the terminal to see the difference, as shown here:

//output of Laravel 5.8 php artisan serve command ss@ss-H81M-S1:~$ cd code/laravel58 ss@ss-H81M-S1:~/code/laravel58$ php artisan serve Laravel development server started: [Wed Apr 3 08:23:31 2019] Failed to listen on 127.0.0.1:8000 (reason: Address already in use) Laravel development server started: [Wed Apr 3 08:23:49 2019] 127.0.0.1:44394 [200]: /favicon.ico [Wed Apr 3 08:24:40 2019] 127.0.0.1:44408 [200]: /favicon.ico

In the previous code, these two lines are important:

[Wed Apr 3 08:23:31 2019] Failed to listen on 127.0.0.1:8000 (reason: Address already in use) Laravel development server started:

408 Appendix More New Features in Laravel 5.8

Since port 8000 is already in use, Laravel 5.8 is smart enough to start another server on port 8001. According to the new features of Laravel 5.8, it will scan up to port 8002 and try to find a port that is free, as shown in Figure A-6. This is a great advancement because now locally you can run multiple applications at the same time.

Figure A-6. Along with two terminals, two browsers running in parallel on two ports

There is another improvement in the of Artisan::call. You use the Artisan::call method when you want to call the method programmatically. In the past, you could pass some options to the command this way:

Artisan::call('migrate:install', ['database' => 'laravelpractice']);

But with Laravel 5.8, this has changed drastically. Now Laravel imports the console environment and uses flags just like artisan commands in the terminal.

Artisan::call('migrate:install –database=laravelpractice');

409 Appendix More New Features in Laravel 5.8 A Few More Additions

The following are a few other additions.

Renaming the Mail Format Folder

You learned about e-mail verification in Chapter 10. The e-mail validation methods have been improved a lot. This is especially true when you have mailable classes in your project, as you saw in Chapter 12. I usually customize the components with the vendor:publish artisan command. In earlier versions, the created directory would be named /resources/views/ vendor/mail/markdown; it is now named /resources/views/vendor/mail/text. The logic behind this is simple but meaningful. Both folders can contain Markdown code for making good-looking, responsive HTML templates with plain-text fallbacks. That is why the markdown folder has been renamed to text.

Changes to .env

A major change has taken place in the .env file. Laravel 5.8 uses the relatively new DOTENV 3.0 to manage the .env file. It will be an extremely important change in Laravel application development in the future. Why? Let me explain. Until now, Laravel’s .env does not support multiple lines and whitespace. The new features of DOTENV 3.0 support multiline strings. So, inside the .env file, you can write like this:

DEVELOPMENT_APP_KEY="multiline strings"

Only the first line would have been parsed before. Now the whole string will be parsed. That means when implementing security in the future, you can now ask for multiline API keys.

410 Appendix More New Features in Laravel 5.8 Changing the Look of Error Pages

There has been a big change in how error pages look. Figure A-7 shows an error page in a Laravel 5.7 application.

Figure A-7. Error page in Laravel 5.7

Some say that the new look is more modern, although you may disagree (Figure A-8).

411 Appendix More New Features in Laravel 5.8

Figure A-8. Error page in Laravel 5.8

Improving Array and String Helper Functions

Array and string helper functions are going to be improved soon. The array_* and str_* global helpers have been deprecated already and will be removed in Laravel 5.9. There are Arr::* and Str::* facades that will be used instead (although there are packages that you will be able to use to maintain functionality if you don’t want to change the existing code).

Changes in Caching

In the previous versions, caching was set in minutes, as shown here:

// Laravel 5.7 - Store item for 10 minutes... Cache::put('foo', 'bar', 10);

412 Appendix More New Features in Laravel 5.8

Laravel 5.8 uses seconds, so now you have to multiply 10 by 60 and change the previous code to this:

// Laravel 5.8 - Store item for 10 minutes... Cache::put('foo', 'bar', 600);

There are some other minor improvements in Laravel 5.8. Please consult the documentation for the full list.

Where to Go from Here

You have learned about many important concepts in Laravel 5.8 in this book. I have used several different projects to explain Laravel concepts. Although you did not develop a complete project from beginning to end, you can find the full code of all the projects discussed in the code repository. Please download the code files for this book while you read and practice. I have used the company/project/task management application to explain all the model relations along the authorization, authentication, and middleware. Since Laravel has great support for the JavaScript framework Vue.js, I suggest you learn at least one JavaScript framework to facilitate more accomplishments in the future. Learning a PHP framework like Laravel opens up many doors. I hope you will be able to use this knowledge base to move forward and develop some awesome applications.

413 Index

A ArticleController update() method, 170, 171 AboutTheUser event, 353 Article management application, 108, 109 Abstraction, 294–296 Artisan::call method, 409 API authentication artisan commands, 13, 14, 229, 361, browser URL, 398 407–409 login credentials, 396, 397 Authentication login screen, 397 articles, 239 OAuth client, 395, 396 blog link, 238 API creation HomeController, 237 Article API, 368, 369 middleware mechanism, 228 ArticleFactory, 369, 370 administrator dashboard, 233, 234 ArticlesTableSeeder, 369, 370 administrator logs in, 233 --collection flag, 371 Blade template page, 231, 232 GET request, 374, 375 CheckRole, 229 JSON response, 377, 378 handle() method, 229 metadata, 384, 385 moderator/editor, 231 Postman, 374, 381 public sections, 237 route list, 373 reviews page, 240, 241 routes register, 372 services, 235, 236 UserCollection class, 381–383 Authentication methods UserResource, 380 in Company/Project/Task app/Repositories folder, 67 Management application AppServiceProvider, 365 add resource, 214–216 ArticleController index method, 375–376 CompanyController.php file, ArticleController.php 217–219, 221 index() method, 106–109 Company model, 222–227 main() method, 109–111 middleware, 216 ArticleController show method, 352, role-based authentication, 216, 217 378, 379 Auth facade, 273, 298 ArticleController store() method, 172

415 © Sanjib Sinha 2019 S. Sinha, Beginning Laravel, https://doi.org/10.1007/978-1-4842-4991-8 INDEX Authorization C blade templates CheckRole, 229 administrator logs in, 266, 267 Composer installation create code, 273, 274 global, 12 delete() method, 268 laravel edit.blade.php page, 275, 276 global, 13, 14 edit companies page, 278 local, 14, 16 index method, 272 local, 11 static methods, 273 Contracts vs. facades, 297 store() method, 275 attempt() method, 300 update() method, 277 Auth facade, 298 view pages, 268–271 CRUD operations, 298 handle method, 259 SOLID design principle, 300 home.blade.page code, 261–266 store() method, 300 HomeController, 256 Controller class, route, 48–51 policies working database/migration folder, 57, 58 app/Policies directory, 288 Model class, 58, 59 AuthServiceProvider, 290, 292 TaskController.php file, 52–56, 60–61 functionalities, 285, 286 task resource, 61, 62 mod policies, 289, 290 task model, 56 redirectTo method, 258 Create-read-update-delete (CRUD) services, 235, 236 approach, 43, 207 using Gates and Policies CSRF token validation, 2 administrator page, 279, 280 navigation bar, 283–285 B Data access object (DAO), 10 back() helper method, 167, 168 Database-driven application, 37, 81, 83 Blade template Database handling app.blade.php file, 69 artisan tinker command, 207 authentication, 70 CRUD operation, 209 automatic caching, 68 destroy() method, 210 control structures, 71 eloquent models, 208 foreach loop, 72, 73 Listtodo, 208 PHP isset function, 72 save() command, 208 resources/views/tasks/index.blade. Database integration, 104 php file, 68 delete() method, 268 security, 70 Dependency injection (DI), 3, 65, 132, 301 416 Index

Dependency inversion principle, 295 one-to-one Dump server article model, 116, 117 composer.json file, 399 content management output, 400, 401, 405–407 application, 120 routes/web.php file, 402 hasOne('AppArticle') method, 117 user data, 402–404 welcome.blade.php code, dump-server command, 402, 404 118, 119 Events, 341, 343 Events and broadcasting, set up E autogenerate events, 366 edit() method, 173, 268 create events (see Events creation) Eloquent model, 75, 79, 109, 114 event classes, 343 Eloquent ORM receiving messages (see Messages, app directory, 114 receive) database migration, 115 software design pattern, 342 Eloquent relation Events creation has-many-through AboutTheUser, 353 article model, 141, 142 controller methods, 343–346 countries table, 137 CreatemessageController, 346 country model, 136, 137 EventServiceProvider, 354, 355 resources/views/articles/show. listeners, 354, 355 blade.php, 139 listen property, 357 show() method, 139 message model, 346, 347 user model, 138 messages, create, 350, 352 many-to-many, 132 messages, display, 351 article_id/tag_id, 134 resourceful controller, 343 article model, 133 routes register, 351 belongsToMany method, 134 show template, 349 database/seeds/DatabaseSeeder. SOLID, 357 php file, 135 user model, 347, 348 Faker object, 135 show() method, 133 one-to-many F articles.blade.php, 126 Facades, 297 articles method, 122–125 factory() methods, 94 routes/web.php code, 121 Faker object, 92, 94, 96, 97 User model, 127 Forge server, 18

417 INDEX Form inputs, traditional way L action part, 200–202 LAMP server, 13 article.store, 195 LAMP technology, 9, 19 code, article.create Blade page, 190, laraeventandlisteners, 352 192, 194 Laravel, 292 create content, 189, 190 composer dependency manager, 3 edit part, article.edit Blade page, DI, 3–5 196–200 features, 3 Form::model method, 187 flexibility, 1 Form request validation framework components, 6 custom message, 204 HTTP layers, 2 form request class, 202 IoC container, 4, 5 rules() method, 203 MVC pattern (see Model-View-­ unauthorized page, 204, 205 Controller (MVC)) PHP framework, 1 G route and middleware request, 6 getRouteKeyName method, 79 laravelcollective/html package, 181 Laravel , 341 H Laravel 5.8 caching, 412, 413 handle() method, 229, 259 .env file, 410 hasMany relationship, 126 error pages, 411, 412 Homestead helper functions, 412 configuration, 27, 28 mail format, 410 installation, 25–27 Laravel model, migrations and MySQL workbench, 34, 35 AppProfile model, 87 and shared folders, 28–31 code functionality, 85 box comments table, 86, 92 installation, 19–22, 24 config/database.php file, 81, 83 launching, 31–34 database/migrations folder, 85 virtualbox installation, 19–22, 24 eloquent ORM, 83 htmlspecialchars function, 70 password-resets, 84 profile model, 84, 86 I, J, K role model, 90, 91 index() method, 105, 106, 245, 268 tags and articles model, 87–89 interface keyword, 296 user model, 86, 91 Interface segregation principle, 295 Laravelmodelrelations, 83, 84

418 Index

Laravel Passport email verification, 313, 314 AuthServiceProvider, 390, 391 MustVerifyEmail, 315 composer , 386 nullable timestamp, 317, 318 encryption keys, 387 user model, 314 home.blade.php file, 392, 393 use tinker, 319, 320 npm install command, 388, 389 sending e-mails OAuth client, 394, 395 API-based services, 324 oauth_clients, 387, 388 HomeController, 329, 330 secured delegated access, 386 Mailtrap, 324, 325, 327, 332–334 service provider registers, 387 SendMailable class, 328, 329 TokenGuard, 391 user verification, 331 User model, 389, 390 send notifications Vue components, 392 Mailtrap, 334 Laravel project, creation, 37–39 NewuserRegistered.php file, 335, Laravel service container 336, 338 and classes Mailtrap, 324, 327 app() function, 312 make:controller–resource bind method, 308 command, 268 bind interface, 309 make:resource Artisan chained instances, 306, 308 command, 371 make() function, 312 markdown flag, 361 method chaining, 308 Messages, receive DBUserRepository.php, 302 alert users, 358 defined, 301 AppServiceProvider, 365 method injection, 303–305 MailForNewMessage, 361, 362 raw data output, 304, 305 Mailtrap, 360, 363 UserController, 303 multiple events, 364 UserRepository, 305 new-message blade template, Liskov Substitution principle, 295 361, 362 Live notifications, 342 SendMailForNewMessage, 359, 360 $subscriber property, 363 M Vue, 358 Mail template Method injection, 66 local development middleware(), 257 change route, 321, 322 Migration database/migrations/user database/migrations directory, 114 table, 316 tests table, 114

419 INDEX

Model methods, 245 database and eloquent, relations, ReviewController, 252, 254, 255 104, 105 show.blade.php code, 250, 251 home page workflow, 7–9 article management Monolog PHP package, 16 application, 98 articles and tags model, 99 N code listings, 100 comment methods, 101 Nexmo, 334 profile model, 102 Notifications, 341 user and tags model, 101 notify method, 342 user model, 102, 103 relations (see also Laravel model, O migrations) Object interfaces, 296 comment object, 80 Object-relational mapping (ORM), 83 faker object, 92 Observer pattern, 342 MVC pattern, 80 Open-closed principle, 294 users database table, 80 route (see Route model binding) Model relations, features P, Q ArticleController.php, 156–158 paginate() method, 376 comment object, 166 Passport::routes method, 390 show.blade.php page, 159–161 Polymorphic relations show() method, 158, 164 commentable_id column, 143 user’s profile page, 161–166 commentable_type column, 143 Model-View-Controller (MVC), 41 comment model, 151 architecture, 7 comment table, 142, 144–146 ArticleController show($id) comment table, fake data, 155 method, 252 Laravelmodelrelations, 155, 156 Article model, 241–244, 246 PHPMyAdmin, 154 Article views, 247, 248 problem, 143 articles index page, 249 profile model, 147 controller, 9 user model, 147 DAO, 10 user factory, 149, 150 framework, 10 PsySH, 207

420 Index R implicit, 78, 79 RESTful action, 77, 78 Read-eval-print-loop (REPL) RouteServiceProvider, 174 mechanism, 207 redirect() function, 167–168 Redirect methods S action method, 169 Separation of concerns back()->withInput() method, 168 composer.json file, 129 flash-data, 171, 172 dependency injection, 132 redirect() helper, 168 resources/views/users/index.blade. RedirectResponse instance, 169 php code, 131 route method, 169 User controller, 130 redirectTo method, 258, 260 UserRepositoryInterface, 129 Redirector, 169 Service container, 308 Representational State Transfer (REST), ShouldBroadcast interface, 341 367–368 show() method, 105 Request object, 172, 174 Single responsibility principle, 294 Resourceful controllers Slug, 79 app/HTTP/Controllers directory, 64 SOLID design principle default set, 64 dependency inversion, 295 index method, 63 interface segregation, 295 TaskController, 63 Liskov Substitution, 295 Route open-closed, 294 action/controller methods, 44–48 single responsibility, 294 concept, 41, 42 SQLite database, 211, 212 controller/view, creation (see store() method, 268, 275, 300 Controller class, route) $subscriber property, 364 list, 44 RESTful/resourceful controllers, 43 user input/dependency injection, T 65–67 table() method, 246 route facade, 297 TaskController show method, 75 route() method, 275 Task model, 51, 75 Route model binding Tinker custom, 79 database handling, 207–211 database operation, 75 PsySH, 207 form template, 76 toArray method, 372

421 INDEX U vendor:publish artisan command, 410 View pages, 42 update() method, 173, 268, 277 Vue.js, 343, 392 up() method, 84 URI segment, 79 W, X, Y, Z Web form V form inputs (see Form inputs, Valet, 18 traditional way) Validation form tags, 181 ArticleController, 176 Laravel HTML and Form packages attributes, 176 form fields, 184 bail rule, 177 installation, 182 create.blade.php file, 179, 180 providers array, 182 $errors variable, 178 upload images, 185 nullable, 178 model binding, 186, 187 ValidatesRequests, 175 Form::model method, 187 Validator facade, 180 submit button, 189 view template, 176 text area, 189

422