1

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!

Sergiu Pirlici
  • 140
  • 2
  • 13
  • 1
    The themes in the post you are referring to are HTML templates. The CSS files have to be located in the `public` folder – Patrick Reck Jun 02 '14 at 21:06

3 Answers3

2

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.

Martijn Thomas
  • 861
  • 3
  • 13
  • 24
  • Ok MThomas, I created an controller named **`CssController`** in wich I have a method **`loadCss()`**, I created a route **`Route::get('style.css', 'CssController@loadCss');`** - all works ok when I access css file _lux-apps/style.css_. However... there is an another problem - I want to my css files to be accessed like here: _lux_apps.ru/css/style.css_, I created a route **`Route::get('css/style.css', 'CssController@loadCss');`** and **IT DOESN'T WORK!!!** :( Why so? can you help me? – Sergiu Pirlici Jun 03 '14 at 19:48
  • Frist of all, I meant that the solution using the controller is a bit messy, the proper way would be storing your files in public/theme/css. Do all other routes function well, it sounds like your Apache mod rewrite is not configured correct or your using the wrong htaccess file. – Martijn Thomas Jun 04 '14 at 09:00
1

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

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

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.

Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307