Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set view file path in laravel?

Tags:

laravel

I have a directory structure for laravel app like this:

app/
   admin/
      controllers/
      views/ -> for admin views
   ...
   views/ -> for frontend views

How can I set the view path for controllers in admin? I don't want to use View::addLocation or View::addNamespace because I might have the same view file name for admin and frontend, and don't want to add a namespace for every View::make('namespace::view.file').

I see in http://laravel.com/api/4.2/Illuminate/View/View.html there is a setPath method, but how do I call it? View::setPath raised undefined method error.

like image 572
xdim222 Avatar asked Dec 13 '14 11:12

xdim222


People also ask

How do I change the view path in Laravel?

Every Laravel config can be changed at runtime using the Config::set method. Config::set('view. paths', array(__DIR__. '/../admin/views'));

Where is the default location for the views of Laravel?

Views are stored in the resources/views directory. return view('greeting', ['name' => 'James']); }); As you can see, the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory.

How do I change the default view in Laravel?

php use Illuminate\Support\Facades\Route; //here we can set our default view Route::get('/', function () { return view('sample'); }); Output: Hello world!

How do I load a view in Laravel?

Loading a view in the controller is easy. You just need to add `view('viewname')` method while returning from the controller method. `view()` is a global helper in Laravel. In this method, you just need to pass the name of the view.


1 Answers

You have two ways to accomplish your goal. First, let's have a look at app/config/view.php. That's where the path(s) for view loading are defined.

This is the default:

'paths' => array(__DIR__.'/../views'),

Method 1: Load both directories

You can easily add the admin directory to the array

'paths' => array(
    __DIR__.'/../views',
    __DIR__.'/../admin/views
),

Now the big disadvantage of this: view names have to be unique. Otherwise the view in the path specified first will be taken.
Since you don't want to use a view namespace I suppose you don't want a syntax like admin.viewname either. You'll probably like method 2 more ;)

Method 2: Change the view page at runtime

Every Laravel config can be changed at runtime using the Config::set method.

Config::set('view.paths', array(__DIR__.'/../admin/views'));

Apparently setting the config won't change anything because it is loaded when the application bootstraps and ignored afterwards.

To change the path at runtime you have to create a new instance of the FileViewFinder.
Here's how that looks like:

$finder = new \Illuminate\View\FileViewFinder(app()['files'], array(app_path().'/admin/views'));
View::setFinder($finder);

Method 3: Use addLocation but without default path

You could also remove the default path in app/config/view.php

'paths' => array(),

And then use View::addLocation in any case (frontend and admin)

View::addLocation(app_path().'/views');
View::addLocation(app_path().'/admin/views');
like image 80
lukasgeiter Avatar answered Nov 10 '22 00:11

lukasgeiter