Loading…
Close ×
Inquiry
×

Inquiry

Inquiry

Get A Free Quote

Let’s get to work! Contact us with the form below at no obligation to you. Together, we’ll set the project’s scope and demonstrate you value that the experts at cmsMinds bring to projects like yours.

Tell Us A Bit About Yourself

    Select As Many Of These As Apply

    newsletter

    Laravel 9, what makes the transition worth it?

    by cmsMinds May 02, 2022

    Laravel 9, what makes the transition worth it?

    Laravel has been leading and widely used PHP frameworks ever since its inception. It’s adaptive, scalable, and versatile, and has become one of the go-to platforms for PHP developers and businesses. Laravel released V9 on 8 February 2022, and it comes with a slew of new features. It’s no wonder that Laravel has become one of the most popular PHP frameworks for creating unique and resilient online apps.

    The new upgrade automatically prompts queries such as –

    • What are the new features in Laravel 9?
    • What are the new updates in this version?
    • Shall a Laravel developer transition from 8 to 9?

    This blog aims at demystifying all of the above questions and more! But, what is Laravel, and what are the features that make it so famous?

    Laravel and its Features

    • It is an open-source PHPframework that is free to use, and one can contribute to the development of the framework.
    • Various features that make developing web apps and APIs simple, secure, and effective include-.
      • Artisan CLI allows the creation/modification of any part of Laravel   based on commands.   
      • CLI lets seeding and migration of databases get effectively done.
      • ORM of Laravel allows seamless interaction with the data model     and database.
      • Laravel follows a common web development pattern with continuing, major enhancements, its MVC architectural structure makes it relatable and flexible.
      • Due to its commitment to the OWASP security standards, Laravel is a highly secure framework.

    Laravel receives regular updates that provide new features, keeping it up to date.

    All about Laravel 9

    Originally scheduled for September 2021, Laravel 9 is believed to be the first LTS (Long Term Support) release in a 12-month cycle. Laravel depends on various community-driven and symphony9 libraries, and symphony-a web app PHP framework-had planned to release Symfony v6.0 by November 2021, which led the Laravel team to delay the release of Laravel v9.

    Laravel 9 incorporates a bunch of new features, some of them include-

    • Minimum PHP Requirements
      • Laravel 9 requires the latest PHP 8 and PHP Unit 8 for testing because Laravel uses Symfony v6.0, which requires at least PHP 8.
      • PHP 8 comes with the new just-in-time (JIT) compiler, the OPcache extension, named arguments, etc.
    • Anonymous Stub Migration
      • The anonymous stub migration functionality was initially introduced in Laravel 8.37 to address the issue that several migrations in the same class name might cause problems when attempting to restore the complete database.
      • The stub migration feature eliminates migration class name collisions.
    	
    	use Illuminate\Database\Migrations\Migration;
    	use Illuminate\Database\Schema\Blueprint;
    	use Illuminate\Support\Facades\Schema;
    	return new class extends Migration {
    	    /**
    	     * Run the migrations.
    	     *
    	     * @return void
    	     */
    	    public function up()
    	    {
    	        Schema::table('people', function (Blueprint $table)
    	        {
    	            $table->string('first_name')->nullable();
    	        });
    	    }
    	};
    	
    

    When Laravel 9 launches, this will be the default when you run

    • New Query Builder Interface
      • Laravel 9 features the new query builder interface introduced by Chris Morell.
      • This makes type hinting highly reliable for refactoring, static analysis, and code completion in their IDEs.
      • With this feature, Laravel 9 developers can now enjoy the new query builder interface for type hinting, refactoring, and static analysis.
    	
    	return Model::query()
    	->whereNotExists(function($query) {
    		// $query is a Query\Builder
    	})
    	->whereHas('relation', function($query) {
    		// $query is an Eloquent\Builder
    	})
    	->with('relation', function($query) {
    		// $query is an Eloquent\Relation
    	});
    	
    
    • This version adds the new Illuminate\Contracts\Database\QueryBuilder Interface, as well as the Illuminate\Database\Eloquent\Concerns\DecoratesQueryBuilder, the trait that will implement the interface in the place of the _call magic method.
    • New Test Coverage Option
      • A new artisan test –coverage option will display the test coverage directly on the terminal.
      • It also includes a –min option that you can use to indicate the minimum threshold enforcement for test coverage.
    • PHP 8 String Functions
      • Since Laravel 9 uses PHP 8, developers can work with PHP 8 string functions.
      • These functions include the use of str_contains(), str_starts_with(), and str_ends_with() internally in the \Illuminate\Support\Str class.
    • New Helpers
      • Laravel 9 introduces two new, convenient helper functions that you can use.
        1. The tr function

    This function returns a new Illuminate\Support\Stringable instance for the given string. This function is equivalent to the Str::of method.

    	
    $string = str('Jai')->append(' Hind');
     
    // 'Jai Hind'
    	
    
    1. The to_route function

    This function generates a redirect HTTP response for a given named route, providing an expressive way to redirect to named routes from your routes and controllers.

    	
    		$string = str('Jai')->append(' Hind');
     
    		// 'Jai Hind'[/php]
    	
    
    •  New Design for routes: list
      • The route list command has been there in Laravel for a long time but one issue that sometimes arises is if you have a huge and complex route defined, it can get messy trying to view them in the console.  Thus, it has been given a makeover as shown below.

    These were some unique features of Laravel 9 that make it distinctive from the other versions. However, why should you upgrade to Laravel 9? Is Laravel 9 a significant improvement over previous versions? The following section will answer these questions.

    Laravel 9, why is it better than Laravel 8?

    • Flysystem 2.0 

    Flysystem is a file storage library for PHP. It provides a single interface for interacting with many different types of filesystems. Laravel 9 has migrated to Flysystem 2.0 from Flysystem 1.0. Frank de Jonge, the creator of Flysystem says,

    ”For the second version of Flysystem, I went back to the drawing board. Many of the library’s core design elements have been brushed up and improved. The API is reduced while keeping the same functionality. Error handling is now purely exception-based, and directory listings are now backed by generators. Although there are many changes, this version of Flysystem is true to its roots.”

    In V1, the failure exception design had two different paths to deal with the errors.

    • For an unsuccessful operation ’false’ was used as an indicator, which led to various complexities in the Flysytem and the code consuming it.
    • For unexpected errors, exceptions were used.
    	
    		try {
    		  $success = $filesystem->write($path, $contents);
    
    		  if ($success === false) {
    		    // handle error
    		  }
    		} catch (Throwable $exception){
    		  // handle an exception
    		}
    	
    

    In V2, all mistakes are expressed as exceptions.

    • Every filesystem operation has a corresponding exception class.
    • Each exception class indicates what the original operation was and what went wrong.
    • If an underlying SDK or client library throws an exception, Flysystem will encapsulate it in a Flysystem-specific exception while retaining the stack trace.

    It makes exception handling in Flysystem 2.0 simple and effective.

    	
    		try {
    		   $filesystem->write($path, $contents);
    		} catch (UnableToWriteFile $exception) {
    		 // handle the error
    		}
    	
    

    In V2, all writes and deletes are deterministic as the ‘update’ function has been removed to make the code consumption simpler and to avoid unnecessary checks, which often resulted in expensive calls over the network. 

    	
    		//Flysystem V1
    		if ($filesystem->has($file)) {
    		  $filesystem->update($file, $contents);
    		} else {
    		  $filesystem->write($file, $contents);
    		}
    		//Flysystem V2
    
    		$filesystem->write($path, $contents);
    
    		// if you REALLY need to check if a file exists
    		$fileExists = $filesystem->fileExists($pathToFile);
    	
    

    Directory listing in Flysystem 2.0 is backed by generators, which allows for a much more memory-efficient way of listing directory contents. Furthermore, the Flysystem produces a DirectoryListing object, which has some handy methods like ‘filter’ and ‘map’.

    	
    		//Flysystem V1
    		$listing = $filesystem->listContents('path/to/dir');
    
    		$files = array_filter(
    		  $listing,
    		  fn ($i) => $i['type'] === 'file',
    		);
    
    		$paths = array_map(
    		  fn ($i) => $i['path']
    		  $files,
    		);
    	
    
    • Symphony Mailer


    Laravel uses mailer to send emails directly from the script. V9 uses Symphony mailer now which provides more consistency to your application, rather than Swift mailer, which is no longer supported as of December 2021. In an article published by Fabein Potencier-on August 19, 2021- he says,

    “Migrating from SwiftMailer to Symfony Mailer is a relatively easy task as the concepts are the same between the two projects.”

    Thus, it is clear that this transition should be easy and effective. Furthermore, he says,

    ”The fact that it’s under the Symfony namespace does not change anything in terms of separate components. You can use the Symfony Mailer component independently of everything else like you are doing today with Swiftmailer. The great benefit is that I was the only maintainer of Swiftmailer whereas Symfony Mailer benefits from the support of the whole Symfony core team.”

    It conveys that Symphony Mailer is a better alternative to SwiftMailer.

    • Default HTTP Client Timeout

    Laravel 9 incorporates a default HTTP client timeout of 30 seconds. If the server doesn’t respond within 30 seconds, an exception will be returned. This step will help avoid hangs occurring in the previous version. A customized HTTP client timeout can be set by using the timeout method:

    	
    		$response = Http::timeout(90)->get(...);
    	
    
    • The Password Rule

    The methods provided by many classes in Laravel are used to conditionally conduct action if the boolean result of the method’s first parameter returns true or false.

    	
    		$collection->when(true, function ($collection) {
       			$collection->merge([1, 2, 3]);
    		});
    	
    

    Passing this closure to the while or until method in previous Laravel versions suggested that the conditional action would always get executed because the loose comparisons against this closure always returned true, resulting in ambiguous outcomes. This issue is no longer there in Laravel 9.

    These were some high-impact changes that make the transition worth trying. One can check out the Laravel 9 upgrade guide which has step-by-step instructions to upgrade the version and use the new features.

    Conclusion

    Laravel is a fascinating PHP framework that is gaining popularity among developers and Laravel 9 is its best and the latest version to use in web application development, and it has significantly improved the common errors that occured in previous versions.

    The new features of Laravel 9, like Symphony Mailer, Php 8, and Flysystem 2.0, make it more appealing than ever. Bug fixes and additional features are on the way, probably before the mid of August. Thus, the V9 seems to be more reliable and versatile.

    cmsMinds
    Subscribe to our Newsletter

    Top