Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

The Eight Biggest Laravel Development Mistakes You Can Easily Avoid

DZone 's Guide to

The Eight Biggest Laravel Development Mistakes You Can Easily Avoid

Get on track to best-practices with Laravel.

· Web Dev Zone ·
Free Resource

What Is Laravel?

Laravel is a popular, open-source PHP web framework, introduced by Taylor Otwell in 2011 to provide an alternative to the CodeIgniter framework, which did not provide features, such as built-in support for user authentication and authorization.

Without sacrificing application functionality, Laravel creates a pleasing user-experience. However, it is not so easy to work with on new technology. Listed below are some of the most common places for developers to make mistakes while working with Laravel.

Installation and Setup

When you execute the composer install for Laravel 5.6 during installation, it will work perfectly, but when you want to add any other package or for some other reason if we need to run composer update command, then it will not execute. In addition to a server, the Composer will be required, which is a streamlined way of dealing with internal and external dependencies. Otherwise, you will encounter the error: 

laravel is not recognized as an internal or external command.

Solution

During installation it is most important to know basic system requirements and compatibilities. Laravel installation can be done on most servers; the basic server requirements are:

  • PHP >= 7.1.3.

  • A local PHP environment (i.e.Valet, Homestead, Vagrant, MAMP, etc.).

  • A database.

Valet automatically sets up everything for your local PHP environment on Mac and Homestead or any kind of a virtual machine setup for Windows. (Selection is based on the type of OS you are using.)

Laravel Homestead is an official, pre-packaged Vagrant box that provides a stable development environment. (No requirement of Webserver Installation or PHP on a local machine.)

You can install Laravel on a Windows PC via Composer or Homestead.

Installation Via Composer

Laravel used this PHP dependency manager to manage all its dependencies. To use Laravel without any hassle, the Composer must be installed on your machine. Make sure your server meets all of the following requirements:

  • PHP >= 7.1.

  • PDO PHP Extension.

  • XML PHP Extension.

  • Tokenizer PHP Extension.

  • Ctype PHP Extension.

  • BCMath PHP Extension.

  • JSON PHP Extension.

  • Mbstring PHP Extension.

  • OpenSSL PHP Extension.

Once the above requirements are fulfilled, we need to download the Laravel installer using Composer through the command:

composer global require laravel/installer

After that, create the fresh Laravel installation in the specified directory via the laravel new command.

e.g. Directory named buddy will be created via a laravel new buddy command, which is contains a fresh Laravel installation where all dependencies of Laravel are already installed.

Application Key:

If the application key is not set, your user sessions and other encrypted data will not be secure.

Solution

The next thing you should do after installing Laravel is set your application key to a random string. If you installed Laravel via Composer or the Laravel installer, this key has already been set for you by the php artisan key:generate command.

Typically, this string should be 32 characters long. The key can be set in the .env environment file. If you have not renamed the .env.example file to .env, you should do that now. 

Web Server Configuration

If the .htaccess file that ships with Laravel does not work with your Apache installation, it means that Web server configuration is required.

Solution

Laravel includes a public/.htaccess file that is used to provide URLs without the index.php front controller in the path. Before serving Laravel with Apache, be sure to enable the mod_rewrite module so the .htaccess file will be honored by the server.

If the .htaccess file that ships with Laravel does not work with your Apache installation, you should try this alternative:

  • Options + FollowSymLinks -Indexes.

  • RewriteEngine On.

  • RewriteCond %{HTTP:Authorization} .

  • RewriteRule .* - [:%{HTTP:Authorization}].

  • RewriteCond %{REQUEST_FILENAME} !-d.

  • RewriteCond %{REQUEST_FILENAME} !-f.

  • RewriteRule ^ index.php [L].

Note: If you are using Nginx, the following directive in your site configuration will direct all requests to the index.php front controller:

location / 

{

    try_files $uri $uri/ /index.php?$query_string;

}

When using Homestead or Valet, pretty URLs will be automatically configured.

Laravel Nova:

Your requirements could not be resolved to an installable set of packages. The requested package, laravel/nova could not be found in any version; there may be a typo in the package name.

Solution

Add following options at the end of your composer.json-File

"minimum-stability": "dev",

"prefer-stable": true,

and run composer update.


Database Seeds

To make staging environment work conveniently, you might take care of so-called database seeds — the main data, which comes as the first filling the database for the project launch. There will be a chance of something going wrong after when you will move to live server (also called “production”).

Solution

The staging server must be much similar than the live server. The same operating system, web-server configuration, etc. Then, we can minimize the chance of something going wrong after we move to a live server.

A common mistake is for developers to test locally with Windows operating system and Apache web-server (or something like XAMPP); whereas staging/production servers are Linux-based with Nginx server.

Another important note is that the client needs to be heavily involved in this testing. By this point, you should be performing not only technical testing but also business logic — something might be missing or overlooked.

Remove Development Configuration

Sometimes a developer might be rushed and forget to remove development variables and configurations. When they upload these by accident to the production environment, this can be a disaster for a live application.

Solution

It’s important for any developer to have a staging environment that mimics the production environment, which houses the live code. Many new developers try to skip the staging environment and go straight from development to production to save time. This is a mistake because staging can help you identify problems that you didn’t catch in development (remember, staging mimics production).

If you accidentally forget to remove configurations or don’t find bugs until staging, you can still catch them before they hit the production environment.

Backing Up Files

Don’t forget to run backups. Sometimes developers have poor backup practices and don’t back up their data. As a result, they might end up redoing work on a project.

Solution

Sometimes many developers have poor backup practices. Don’t need to back up every hour, but backups should be run each day if you do significant work on a project. Always remember that backups save you hours of recoding, should you lose your data in the event your drive fails

If you have a hard time figuring out a problem in your code, back up the system so you will not lose the solution — and hours of work — and must recode it. A backup can also save you from missing a deadline if something happens to go twisted.

You should also create backups for your clients in the rare case that a client has a critical failure and no backup. It’s a nice gesture, and you can help your client out of a potentially sticky situation.

Migration

To save time in the short-term, most Laravel developers don't use migration features. This is not the correct way to manually create new database tables and directly add new fields into database tables.

Solution

They should use database migration to add or alter anything in the database. Migration is a well-ordered way of specifying table structure without writing a bunch of SQL queries. Even more, migrations work a little like Git, as it allows you to make incremental changes, roll back changes, and keep the database structure in sync when multiple teams are working on the same application. It keeps all record of how the database is created and altered over time; migrations and seeders make it easier to re-deploy the application or distribute it to other people.

Topics:
web dev ,laravel ,php ,xml ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}