Laravel naming convention for blade files [closed]
Asked Answered
P

4

16

I know that naming conventions in Laravel is covered in another question, but blade files are not covered there. I read several blogs and forums and they all offer different approach so I want to ask here:

My controller method is AdminController@listPropertyTypes - which lists and manages the property types..

One blog suggests:

/resources/views/admin/property/types.blade.php

Another blogs suggest underscore or no space:

/resources/views/admin/property_types.blade.php
/resources/views/admin/propertytypes.blade.php

I would personally named this way since it is a view:

/resources/views/admin/property-types.blade.php

Is there a best practice or PSR rule for this?

Prying answered 14/5, 2020 at 22:29 Comment(3)
Best practice? I don't believe so, and PSR rule, these are general rules for PHP standards, Blade is a Laravel specific, so no. As long as you are consistent across your codebase and you/your team are all happy with the convention you decide to use then do whatever you like.Albertina
If you are asking about best practices, then one suggestion would be to strictly use CRUD controllers; AdminController@listPropertyTypes is not CRUD. AdminPropertyTypesController@index is more "best practice".Albertina
I use this convetions github.com/alexeymezenin/laravel-best-practicesPollinate
S
35

EDIT: Laravel community mostly use kebab-case or camelCase

ie views/admin/property-types.blade.php or views/admin/propertyTypes.blade.php

Laravel's creator seems to use kebab-case, but Spatie recommends camelCase.

As @MrEduar explained it, there is no strict convention.

NB: https://www.laravelbestpractices.com website is abandonned and not affiliated with Laravel.


OLD: Initial answer

I came across Laravel Best Practices.

Laravel : Best Practices aims to put together all the resources and best practices in using the Laravel Framework. Last Updated: 2020-05-07 12:26:48

Views

You SHOULD use snake_case as file name of your Blade templates

Good

show_filtered.blade.php

Bad

showFiltered.blade.php
show-filtered.blade.php
Schizomycete answered 15/5, 2020 at 3:41 Comment(5)
Thanks. I'm not that comfortable with this naming convention but I'll go this way.Prying
Hm Taylor himself uses kebab-case like in github.com/illuminate/pagination/tree/master/resources/viewsCelibate
Basically, I usually go for kebab-case for naming my routes as well as the view. It works out pretty well.Venue
github.com/alexeymezenin/laravel-best-practices suggests kebab case namingWitenagemot
laravelbestpractices.com/#views is now an abandonned website. I agree with @Celibate and go with kebab-caseTrumpet
K
4

For blade file names, there is no convention as such. But as @James says in his commentary, and I quote

If you are asking about best practices, then one suggestion would be to strictly use CRUD controllers; AdminController@listPropertyTypes is not CRUD. AdminPropertyTypesController@index is more "best practice".

And in this case the best way would be /resources/views/admin/property/types.blade.php.

You can read more about this in Laracon 2017 or in Adam Watham's github repository where he explains it further.

If you are not happy with this result I suggest you also use CamelCase According to the Spatie Guidelines

resources/
  views/
    openSource.blade.php

So, in the controller

class OpenSourceController
{
    public function index() {
        return view('openSource');
    }
}

Instead of looking at unreliable blogs, be guided by the great minds of the Laravel community.

Kalk answered 15/5, 2020 at 3:54 Comment(1)
Fixed Spatie broken URL (again); and +1 for the "unreliable blogs" warning!Trumpet
A
1

I stumbled on this post because I have a Controller method that returns dynamic view that matches the request path. I've long used the snake_case for my blade files but we all know that route paths could be hyphenated (dashed), and I want them to match the blade files.

Now I'm asking myself if it's okay to ditch the snake_case and dash my-blade files, or should I stick with mr snake and somehow convert the route path to match.

Note. I've tried the dashed-version and things look okay. But I was pricked to check convention, here I am.

Archenteron answered 14/9, 2023 at 5:38 Comment(0)
M
0

There are a few conventions out there that people follow.

snake_case

all_posts.blade.php

camelCase

allPosts.blade.php

kebab-case

all-posts.blade.php 

From my personal preference, I use the kebab-case because

  • it is good for readability and it is visually parse
  • helps when working with web-related assets/resource or routes
  • and also, Spatie and bunch of other developers also recommended this.

Again, Laravel doesn't enforce strict rules, it's a good idea to follow practices used by the community. And once you follow one specific convention, try to stick to that convention throughout the entire application/project.

Manasseh answered 18/4 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.