laravel / ideas

Issues board used for Laravel internals discussions.
940 stars 28 forks source link

Namespaced blade components #1905

Open danswiser opened 5 years ago

danswiser commented 5 years ago

Problem:

I automatically assume that any Blade component fits best in a directory containing all the custom components for a project. For me, they always are in the resource_path('views\components') directory.

Assuming this is true, the first directory is needless when loading any blade component. See the following examples:

@component('components.input.color')
@component('components.input.icons')
@component('components.user.avatar-status')

It would feel much cleaner to me to namespace, if you will, the component directive with it's directory for components. This would allow a simpler selector for components and would look like this:

@component('input.color')
@component('input.icons')
@component('user.avatar-status')

Solution:

I see two ways to solve this.

  1. First Option: Add another directory like resource_path('views\components') to the laravel/laravel view config here. This would be quick and simple but I assume it hurts performance we'd have to search the first directory first for every component.
  2. Better Option: Update the actual component compiler in laravel/framework to append a directory to the view factory here. Something as simple as updating to $this->componentStack[] = 'components.' . $name; would work but I don't know the preferred laravel way for adding something like this.
Miguel-Serejo commented 5 years ago

This will have the same type of debate around it as the idea of namespacing all models to App\Models by default.

While it may be true that in your experience, components all live under the components folder, other architectures exist.

For example, some people prefer to group views by model, rather than type of view:

@component('users.components.description')
@component('users.components.list')
@component('posts.components.comments')

As with the App\Models namespace thing, I see no reason to break BC for this. You can already create your own blade directive that loads components from any directory you want.

martinbean commented 5 years ago

Blade allow you to organise your views and components however you wish. Blade shouldn’t be looking in one directory for one type of view and another for another type of view.

danswiser commented 5 years ago

I know name spacing is a bit of a difficult topic and I get why. Generally I don't support changing defaults. While generating models you can specify your directory for the model and can always import your classes so that you don't have to type out the fully qualified namespace. For those reasons I'm not a fan of changing the default name spacing for models. It's something easy to change and work with.

Unless I've missed something though, there's no good easy way to do the same for blade components.

Option 1 that I mentioned is what I've been doing for a while project by project but doesn't feel like the right way to do it. It doesn't seem smart to search two directories for every component if you have your own dedicated directory. Option 2 requires an update to the core framework. What I'm suggesting is more of a way to customize the default directory of components. Even something like below would add flexibility without hardly any cost.

$this->componentStack[] = config('view.components.path', '') . $name;

Miguel-Serejo commented 5 years ago

You're free to override the component directive in your projects and make it look in whatever folders you'd like, or just prepend a directory name to any component name you pass in.

ManojKiranA commented 4 years ago

From Laravel 7 Laravel Supports Blade Components.

https://laravel.com/docs/7.x/blade#components