DEV Community

Cover image for Developing Quality API with Laravel - Tip #3
Hesam Rad
Hesam Rad

Posted on

Developing Quality API with Laravel - Tip #3

Tip #3 - Extract all your constant values (non-variables) into one config file.

What do I mean by this?

Imagine a scenario where you're developing an API for an e-commerce application. There is going to be model named Product in this application and you (the back-end developer) have to make queries to fetch those products and send them to the front-end client. It would be something like this:

/*
 * Return a list of available products.
 *
 * @return Collection
 */
public function index()
{
    return Product::query()
        ->where('available', true)
        ->paginate(20);
}
Enter fullscreen mode Exit fullscreen mode

And later down the road you have to make another query to fetch all Payment records in your system and it would look like this:

/*
 * Return a list of successful payments.
 *
 * @return Collection
 */
public function index()
{
    return Payment::query()
        ->where('status', 'successful')
        ->paginate(20);
}
Enter fullscreen mode Exit fullscreen mode

And there is NOTHING wrong with this.

I am here to suggest a way to make your codebase cleaner and more maintainable in case of future changes.

Let's say you have around 50 controllers in your Laravel application and all the index methods are returning a collection of 20 items. Imagine if the clients asks you to return only 10 items instead of 20. Now you have to go through all 50 controllers and change ->paginate(20) into ->paginate(10). It's pretty easy right?

If you're using vscode or phpstorm you could ctrl + shift + f to find and replace every ->paginate(20) with ->paginate(10) and your job would be done in a matter of seconds. But that's not a nice way.

This is what I would do and suggest you do as well:

I would create a file called constants.php inside Laravel's config file and put everything I use through out the application inside it:

<?php 

/**
 * Every constant that is used through out the application is 
 * defined here.
 * 
 * Note that if you're caching your application's configuration 
 * you should clear it after each modification to see the change.
 * 
 */

return [

    /*
    |-----------------------------------------------------
    | Items to Paginate
    |-----------------------------------------------------
    | Number of records to paginate in each query.
    |
    */
    'items_to_paginate' => 20,

    //and many other things...
];
Enter fullscreen mode Exit fullscreen mode

Looks clean, right?

Now I would change my controllers like this:

/*
 * Return a list of available products.
 *
 * @return Collection
 */
public function index()
{
    return Product::query()
        ->where('available', true)
        ->paginate(config('constants.items_to_pagiante'));
}
Enter fullscreen mode Exit fullscreen mode

Now if client wants me to change the number of paginated items, I could change one value inside my config file without touching my controllers and the whole system would change with that.

Now this feels clean, right?

Note
Of course you can add as many values as you want. This is not just for pagination. I, personally have a lot of values in my constants.php file; so the usage is up to you.

One more thing, if you're caching your application's configuration you should clear it after each modification to see the change.

Last Words
Hope you found this tip helpful. If you have any suggestions for future tips, please comment down below. I'd be more than happy to get in touch with you.


Link to cover image

Top comments (0)