29

In my Laravel 4 application's root directory, I have a folder themes. Inside the themes folder, I have default and azure. How can I access view from this themes/default folder in a specific route.

Route::get('{slug}', function($slug) {
    // make view from themes/default here
});

My directory structure:

-app

--themes

---default

---azure

I need to load views from localhost/laravel/app/themes/default folder. Please explain this.

Laurence
  • 58,936
  • 21
  • 171
  • 212
Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132

4 Answers4

37

This is entirely possible with Laravel 4. What you're after is actually the view environment. You can register namespace hints or just extra locations that the finder will cascade too. Take a look here

You'd add a location like so:

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

It might be easier if you namespace them though, just in case you have conflicting file names as your path is appended to the array so it will only cascade so far until it finds an appropriate match. Namespaced views are loaded with the double colon syntax.

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

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

You can also give addNamespace an array of view paths instead of a single path.

Arda
  • 6,756
  • 3
  • 47
  • 67
Chirag Shah
  • 1,463
  • 2
  • 18
  • 40
26

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.

Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
  • I appreciate this post, it has a lot of helpful information on it! Can you explain where the View::addLocation() and View::addNamespace() are supposed to go? Am I to assume that this requires creating a ServiceProvider? – Michael Stramel Feb 13 '14 at 20:00
  • Is there a reason why this wouldn't work in a Route filter? I tried it there and it is not working there, but does in global.php. – Patrick Mar 27 '14 at 13:57
  • @pthurmond why would you want to put it in a route filter? – brettwhiteman Jul 08 '15 at 23:43
  • This is the answer https://stackoverflow.com/questions/27458439/how-to-set-view-file-path-in-laravel with perfect description – Morteza Rajabi Jul 31 '17 at 11:19
2

I've developed a theme package for laravel 5 with features like:

  • Views & Asset seperation in theme folders
  • Theme inheritence: Extend any theme and create Theme hierarcies

Try it here: igaster/laravel-theme

igaster
  • 12,983
  • 6
  • 26
  • 27
0

\View::addLocation($directory); works fine but the new right way to do it is using loadViewsFrom($path, $namespace) (available on any service provider).

Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83