Sergiu Pirlici
Sergiu Pirlici

Reputation: 140

How to load CSS and JS from theme directory in Laravel 4

I would be very happy if someone could help me! I'm a beginner with Laravel and started building an application in which I will use themes. I want to my CSS files to be stored in "/path-to-theme/img/" folder. I found some examples refering on CSS loading, but is not what I need.

There is what I found:

<link rel="stylesheet" href="{{ URL::asset('assets/css/bootstrap.min.css') }}">

To see this link for more information. What should I do to load CSS from the "/path-to-theme/img/" instead of "/public" folder.

Please help me! Thank you very much!

Upvotes: 1

Views: 3667

Answers (3)

igaster
igaster

Reputation: 13653

If you have switched to Laravel 5 you can try my package with features like:

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

Try it here: igaster/laravel-theme

Upvotes: 1

Martijn Thomas
Martijn Thomas

Reputation: 951

All Assets must be located in the public directory. Within that directory you can use any folder structure you might want to use eg. public/path-to-theme/etc.

There might be an other solution (didn't test it), but its a bit messy.

You could create an AssetsController and in that controller load the files based on the template settings. I'm not sure how your situation is exactly but I can immagine the following. A user has a preference for a theme (stored in the users table in the DB). In app.php you could create a template config variable. The value of this variable is the default theme that will be used if not a specific theme is set (by the user for example or based on an input field or get parameter (or whatever)).

array(
   'theme' => 'default',
),

In the AssetsController you cold create a getCss action and load the theme corrosponding to the css you need:

public function getCss() {

    $theme = Config::get('app.theme');
    return file_get_contents($theme.'/stylesheet.css');
}

Inside routes.php you create the following route

Route::get('stylesheet.css', array('uses' => 'AssetsController@getCss'));

To load your css in your view file you add this:

<link rel="stylesheet" href="{{ URL::action('AssetsController@getCss') }}">

This works for the default theme. If you want to load the user specific theme preferences you can do that in a filter (filters.php), for example in the auth filter or a custom filter in filters.php (you should append that filter name to the original routes of the pages you want to display).

Route::filter('auth', function()
{
    Config::set('app.theme', Auth::user()->theme);
});

This only works if the user is logged in. But in a different filter you could check if a different parameter is set (an get, post or session variable for example).

Hope this might help.

Upvotes: 2

The Alpha
The Alpha

Reputation: 146191

Assets must published to public folder. You can publish the assets using something like this from your command prompt/terminal:

php artisan asset:publish

php artisan asset:publish vendor/package

php artisan asset:publish --path="vendor/package"

Use the appropriate one. Check the documentation on Laravel website and another related question.

Upvotes: 0

Related Questions