5 mistakes I made learning Laravel

victorighalo
5 min readJul 19, 2018

--

A lot of software developers are introduced to Laravel PHP framework after writing Vanilla PHP(PHP without a framework) for a long time, using their own conventions, patterns and procedures. This makes Laravel not so easy to grasp/understand on first attempts using it. The Laravel documentation also make us developers feel we have wings to fly, then we go ahead and fly in the wrong direction, only to realise this after going very far in the using the framework in a less elegant way.

I wrote this article as a guide for developers new to Laravel, so they get things right the first time they choose make an application with Laravel. If you have not noticed; Laravel was built for Web Artisans — this means it was built for developers who care about writing beautiful and elegant code.

Here’s my list of honest mistakes:

1. Using one Controller for both route requests and view responses.

2. Using one route file to handle all requests.

3. Using database-first instead of code-first approach for database management.

4. Adding front-end dependencies manually.

5. Not taking advantage of VueJS or React.

I won’t just stop at telling my mistakes, so here are my solutions to the mistakes:

First mistake: Using a single Controller

Solutions:

  1. Organise controllers in folders

For a good MVC application structure, controllers should be organised in folders. For example, let’s say your application serves as an api service also, you should have both api and domain folders, then within them you can create specific controllers depending on the nature and scope of the application.

2. Use resource controllers

A resource controller scaffolds the basic action methods needed to make CRUD (Create, Read, Update, Delete/Destroy) operations easily and you won’t have to code routes for each end point. I am guilty of making these action methods manually and naming them all sorts of awkward names, like “GetFavQuote” and “DoGetFavQuote”. It’s not a bad thing but it will make your code dirty and confusing to reason about. With a resource controller you have less to deal with and new developers can easily understand your codes. In other frameworks like ASP.NET there’s a common approach to this which is called the Repository and Unit of Work patterns. You can easily create a resource controller using the Artisan CLI command: “php artisan make:controller Admin/RolesController –resource”.

Take note of the ‘Admin’ folder added to the command, this command will generate a controller at ‘app/Http/Controllers/Admin/RolesController.php’.

Second mistake : Using one route file to handle all requests.

Solutions:

  1. Use Multiple Route files

Don’t be tempted to use the default route file for all routing requests. You can actually have multiple route files, each with their own route prefix. I like to have both admin and user route files, this way I have my routing logic separate and easier to maintain.

It’s very simple to create multiple route files, all you need is just two steps:

  1. Create a new route file — create a new php file in the routes folder and add your routes like you would normally.
  2. Register the route — There should be a CLI command for this, but for now you need to manually tell the framework that you have created a new route file, else it won’t know.

To register your new route, find the RouteServiceProvider.php located in “app/Providers/”.

Add a new method and register it in the ‘map’ method.

Laravel Route Service Provider

Third mistake: Using database-first instead of code-first approach.

The traditional approach for designing databases for an application is to create the database and tables from a terminal or GUI.

With this traditional approach further modifications will be very dis-organized and you will have to go back to your database from time to time to remind yourself of some columns and tables. This may be easy when you’re still developing on your local machine but when the application is hosted on a live server it will become a very stressful process.

Solution:

  1. Use Code-first database approach

With this approach databases and tables are designed by writing codes first, before they are actually created. This is done through something called Migrations. It’s like the git for database designing. It is also a version control for database design.

You might wonder why go through all this when you quickly use PHPMyAdmin to make your tables and columns. Well, there are two major reasons:

a) Easy team work — imagine using the database-first approach to start a project and a new developer joins the project working remotely, and then you have to make changes to the database. Whenever you make such changes, the remote developer will also have to manually go make the same changes exactly, thereby making the whole process slow and error-prone.

With Code-First all you have to do is to update the migration and the remote developer will run the changes and his database will be automatically updated.

b) Version control — Using the Code-First approach, means using migrations, which keeps records of all changes made. This means you have the option to roll-back to previous database states.

You can read more on this here: https://laravel.com/docs/5.6/migrations

Fourth Mistake — Adding front-end dependencies manually

For most projects, web developers require a lot of CSS and JavaScript libraries to make the process faster. Adding these libraries manually by downloading them and referencing each file at the top and bottom of your template files causes serious maintenance and performance issues.

Adding 20 Javascript libraries will mean 20 distinct network requests to download and load them in your application. If one fails then you might have a serious problem with your application.

Adding libraries manually makes it hard for collaborative development. If you push your code to a repository and expect others to clone, how will they know the required libraries? They will have to go through your code and ensure they manually download them and link them, which is a lot of work.

Solutions:

1. Use a front-end package manager like NPM, Yarn or Bower. They basically are repositories for front-end packages, so when there’s an update for a library, you simply run a command and you have the latest. They also keep a manifest of all the files downloaded as part of your application, so anyone can install all dependencies required.

2. Compile all your front-end files using Laravel Mix. With this you can have all your CSS or JavaScript files as one or two files. For more information go here: https://laravel.com/docs/5.6/mix#running-mix

Fifth mistake — Not taking advantage of VueJS

This is not actually a mistake; I will blame this on JQuery. I had gotten so used to JQuery that I did not even consider VueJS. Later down the project i learnt with, I was faced with challenges that would require me to write a lot of JQuery code to implement, so I decided to try VueJS. Boy!! I was blown away at its simplicity and power. I got the feature ready in 2 days, with confidence that my code won’t break (without testing though).

I encourage you to learn VueJS or any other modern JavaScript framework like React or Angular.

Conclusion

Wisdom is the principal thing; therefore get wisdom: and with all thy getting get understanding. Proverbs 4:7 — the Holy Bible.

Learning is a continuous process. Today, we may feel like superstars, tomorrow we find out we haven’t even scratched the surface of knowledge.

--

--

victorighalo
victorighalo

Written by victorighalo

I am a passionate software developer with love for Javascript, C# and PHP. I love to share knowledge and experience. @victorighalo1

Responses (2)