Better Laravel/PHP Code: 5 Guidelines from Spatie



Want your code to be more readable for teammates? Belgian company Spatie has a page full of various guidelines, let’s discuss some of them.

Spatie Guidelines: https://spatie.be/guidelines
My video “Laravel: How to Name Various Things” https://www.youtube.com/watch?v=6f7AvZF8ciY
PSR-2 and PSR-12: Why We Need Standards and How to Apply Them https://blog.quickadminpanel.com/psr-2-and-psr-12-why-we-need-standards-and-how-to-apply-them/

– – – – –
Support the channel by checking out our products:
– Try our Laravel QuickAdminPanel: https://bit.ly/quickadminpanel
– Enroll in my Laravel courses: https://laraveldaily.teachable.com
– Purchase my Livewire Kit: https://livewirekit.com
– Subscribe to my weekly newsletter: http://bit.ly/laravel-newsletter

source
centos 8

Devin BARTON

I am an avid Linux lover and open source enthusiast. I use Ubuntu and believe in sharing knowledge. Apart from Linux, I love classic detective mysteries.

30 thoughts on “Better Laravel/PHP Code: 5 Guidelines from Spatie

  • April 20, 2021 at 11:14 am
    Permalink

    Well, a controller controls all posts, but a model represents only one post, so…

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    You make very straight forward videos..it kinda gives me my motivation to carry on what i m doing..thank you sir !

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    I follow PSR12 with 1 exception – I have every open brace in new line like:
    if ($request->has('file'))
    {
    //
    }
    instead of
    if ($request->has('file')) {
    //
    }
    For me it's more readable

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    thank you. I was very critical of another one of your videos recently, so I felt I should also point out that you also have amazing content like this in your channel. We so often are quick to criticize, yet not so quick to praise. Thank you for this format of a daily video, which is good given how often Laravel changes and evolves. Have a good day

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    I think it's pretty dangerous to spread the idea of trying to avoid comments among newbies/juniors. Cause without well explained examples it could lead to people trying to avoid comments at all cost. Even if they are needed.
    I agree that comments shouldn't be just pointless type hinting or explanation for messy code, that can be cleaned up. And often comments should be used to give context and some kind of documentation. It's more like if you have to ask yourself "what does this code do?", then it's probably a bad code and should be cleaned up. On other hand if you ask "Why this code does something this way?" – it's a good place for a comment.

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    PostsController is the standard in some other frameworks and it's how I used to do it, but in all official Laravel examples it's PostController so I suggest following the official convention as it's more likely to be what other people who work on Laravel projects are used to, and it's more likely to work out of the box with other developer tools that create or work with controllers.

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    We'll no one told me to follow this instructions, but I follow it from the beginning from my own.

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    Another great video! I'm going to have to agree with Spatie and use plural for controllers, since in my head a model is the singular entity, and controller (and tables) manage all of the entities. BooksController@index returns a collection of Book (BooksController@show will show the single). At the end of the day though, as long as the team is consistent for the project, that's all that really matters.

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    "comments should be avoided as much as possible by writing expressive code" – i strongly disagree.
    if it takes me 30 seconds to understand your "expressive" block of code vs 2 seconds to read a comment, then have both: expressive code + comment.

    comments help to skip big blocks of code without even reading them

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    It's off topic, but you're more likely to read it under your latest video:

    I learned from you how to "properly" validate requests, (using make:request).

    But what happens if I have some fields that are optional. I have to, for it to work, in e.g. ClientRequest include it anyway, but set as rule a "blank string". Is this correct?

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    what I dislike about PSR is inconsistency, if I was leading some project I'd put all brackets at the same position, not randomly like PSR suggests

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    Hi, it's not related to this topic, but I would like to ask you:

    I have a function in a controller, where I pass a model as a parameter -> function(User $user) {}
    If I write to URL id of a user, who doesn't exist, it throws an error 404. Is there a way how can I throw my own message? For example, redirect to home page with error message.

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    Only thing I don’t like about ‘defaulting’ to happy paths is that it’s then the implicit behaviour instead of the explicit behaviour. So if something were to go wrong, instead of erroring by default, it executes by default, and I don’t like that design methodology. I agree it’s more readable, but imo it’s more dangerous. But each to their own!

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    Speaking of type-hinting I suggest making a video on php new features like type-hinting, nullable-types, array destructuring …etc i know it's a laravel channel but php is the soul of it

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    Hello. Why I can't make your videos fullsize in my mobile? I try to make the code much visible on the phone but it doesn-t wotk on your videos! Can you look into this issue? Thanls. Alse in you code review videos you say "you can to it in this way too" but you don't show other way. I'm not a Laravel developer but sometimes I do like to watch your videos so it's noce to see "the other way" too.

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    Its quite funny that you always search something lmao.

    You say make your code shorter. But now you read SPATIE's guidelines and you say: "I see junior devs use this if statement to shortener the code"

    Reply
  • April 20, 2021 at 11:14 am
    Permalink

    thanks sir for your great content

    -when we create a controller with its own resources – r by default for each function it gives 4 line of comments, is there a way to disable them to not be written?

    -which PSR-? version you are using in your ide?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Buca escort