How to Load View from Alternative Directory in Laravel 4

How to load view from alternative directory in Laravel 4

Here I am not accessing my project from public folder. Instead of this I am accessing from project root itself.

I have seen a forum discussion about Using alternative path for views here. But I am little confused about this.The discussed solution was,

You'd add a location like,

View::addLocation('/path/to/your/views');

Then add namespace for theme,

View::addNamespace('theme', '/path/to/themes/views');

Then render it,

return View::make('theme::view.name');

What will be the value for /path/to/ ?

Can I use the same project in different operating system without changing the path?

Yes, we can do this using the following,

Put the following in app/start/global.php

    View::addLocation(app('path').'/themes/default');
View::addNamespace('theme', app('path').'/themes/default');

Then call view like the default way,

    return View::make('page');

This will render page.php or page.blade.php file from project_directory/app/themes/defualt folder.

Laravel Different Views Folder

Thanks but the answer I was looking for is in here
https://github.com/Shipu/themevel
Anyway, thanks again

How to set view file path in laravel?

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');

Where in Laravel 4 to declare new location and new namespace

If using the app/config/view.php configuration file to add view loading locations (via the paths array) is not enough for your needs, you can probably fit that into a service provider.

Laravel actually uses the View Library's Service Provider to register the view paths locations (based on the app/config/view.php config file as mentioned).

One thing you can do is add your own service provider class and add in your view logic there, in order to add a location / namespaces as you need. (You can even have your service provider read your own configuration files in order to determine locations/namespaces).

If you need help creating a service provider/don't know where to put one, read this on creating a Laravel application library.

Laravel 7: Generate named view-routes from blade files in directory

If I understand correctly, what you you need is a generic get route like this:

Route::get('/{page}', 'PageController@show');

and then you need a PageController with a function to return the requested page:

public function show($page)
{
return view('project.content.'.$page);
}

Just have in mind that this kind of route will "catch" every get request so put it at the end of the web.php file

Extend blade engine in Laravel to use different views directory

You could probably append the path to the configuration:

1) Statically, by modifying file config/view.php

'paths' => [
realpath(base_path('resources/views')),
//more paths here
],

2) Dynamically at runtime:

$paths = config('view.paths');
$paths[] = $newPathToAdd;
config(["view.paths" => $paths ]);

I suggest you use this in moderation otherwise you will just end up with a mess of directories with no real specified purpose.



Related Topics



Leave a reply



Submit