15

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?

tolga
  • 2,462
  • 4
  • 31
  • 57
  • 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. – James May 14 '20 at 22:31
  • 1
    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". – James May 14 '20 at 22:32
  • I use this convetions https://github.com/alexeymezenin/laravel-best-practices – Arash Younesi Mar 24 '21 at 19:14

2 Answers2

31

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
4wk_
  • 2,458
  • 3
  • 34
  • 46
Digvijay
  • 7,836
  • 3
  • 32
  • 53
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.

4wk_
  • 2,458
  • 3
  • 34
  • 46
MrEduar
  • 1,784
  • 10
  • 22