Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a hint path for laravel blades

If you go into an error blade file for example 403.blade, you will see it uses construction like:

@extends('errors::layout')

I would like to use the same construction or at least understand how it works.

so I'm trying from a controller

return view('role::account.list');

and getting exception

 No hint path defined for [role].

I knew it was not defined, but how can I define it?

Thank you.

like image 796
Yevgeniy Afanasyev Avatar asked Oct 20 '25 05:10

Yevgeniy Afanasyev


2 Answers

The easiest way is to use a ServiceProvider and a loadViewsFrom call:

https://laravel.com/docs/5.6/packages#views

/**
 * Perform post-registration booting of services.
 *
 * @return void
 */
public function boot()
{
    $this->loadViewsFrom(__DIR__.'/path/to/views', 'courier');
}

Will allow you to use view namespace courier:

Route::get('admin', function () {
    return view('courier::admin');
});

You can also use the logic that is running behind the scenes, by using the View factory which uses the FileViewFinder:

app('view')->addNamespace('role', resource_path('views/role'));

There are more methods available either via the Factory of via the FileViewFinder, you can retrieve the finder like this:

app('view')->getFinder();
like image 187
Luceos Avatar answered Oct 22 '25 05:10

Luceos


Add hint path like so:

app('view')->addNamespace('role', config('view.paths')[0].'/roles/visitor');

you can do it in a controller or Middleware.

And If you want to change it later just do this:

app('view')->replaceNamespace('role', resource_path('views') . Auth::user()->role->view_path);

Yes it is ridiculous that the error says "hint path" and a function named "addNamespace". And it is upsetting that this is NOT a part of documentation.

like image 43
Yevgeniy Afanasyev Avatar answered Oct 22 '25 05:10

Yevgeniy Afanasyev