Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Shared assets themes
  • Hi everybody,
    I have several applications built with fuel with a backend.

    My goal : share admin theme views and admin theme assets on the same server between apps.

    I already share my admin theme views.

    First, folder structure :
    -- fuelphp (outside DOCROOT)
    ----1.7.1
    -------- core
    -------- vendor
    ----shared
    -------- packages
    -------- modules
    -------- themes

    -- www (Server DOCROOT)
    ---- app1 (http://app1.local)
    -------- index.php
    -------- assets (css, js, img, themes)
    -------- app (classes, config, modules........)
    ---- app2 (http://app2.local)
    -------- index.php
    -------- assets
    -------- app (classes, config, modules........)
    ---- shared_assets (http://shared.assets.local)
    -------- css
    -------- js
    -------- img
    -------- themes
    ---------------- admin
    -------------------------- css
    -------------------------- js
    -------------------------- img

    In app1/index.php :
    define('SHAREDPATH', realpath(__DIR__.'/../../fuelphp/shared/').DIRECTORY_SEPARATOR);

    In config/theme.php :
    return array(
         ....
        'paths' => array(
            APPPATH.'..'.DS.'themes',    
            SHAREDPATH.'themes',      //location of my admin theme views
        ),
        ....
    );

    But I don't know how to share admin theme assets.

    I try this technique
    http://fuelphp.com/docs/classes/theme/advanced.html#/advanced_config
    to add new asset base url.

    In my Base Controller Template :
    public function before()
        {
            $theme = \Theme::instance();
            $theme->active(array(
            'name' => 'admin',
            'path' => SHAREDPATH . 'themes/',
            'asset_base' => 'http://shared.assets.local',
            ));
            ......
        }
    But it doesn't work, i have always http://app1.local (Config::get('base_url')) like base url.

    For global assets, i changed base url on the fly with Asset class.

    In my Base Controller Template
    :
    \Asset::forge('shared', array(  'url' => 'http://fuelphp.shared.local:8080/'));

    And in the view theme :
    echo \Asset::instance('shared')->css(array('bootstrap.css', 'font-awesome.css'));

    This works!

    So how configure theme class to add new base url asset?
         

  • HarroHarro
    Accepted Answer
    Your asset base URL should always be your app URL, as it is used by the client.

    And that is the problem here, it is the browser that fetches the assets, through links in the html page. This means that they should be accessable by the browser, which means they should be in ./public (app1, app2, etc in your case).

    What you essentially do is have a second host to serve your shared assets, and that is what the Asset class can't do. It can service assets from multiple locations, but through a single URL. Not the other way around.

    They only way to solve this (that I can think of atm) is to use two Asset instances, one for your app, one for your shared assets. For both you can configure a different base url, which solves your problem.

    If you absolutely need theme support for shared assets too, I don't know an immediate solution, as the theme class doesn't support multiple asset instances afaik.
  • Thanks Harro for your analyze and your advices.

    I decided to create 2 Asset instances, and it works!

    In my public controller
    \Asset::forge($this->theme, array(    'url' => 'http://fuelphp.shared.local:8080/themes/' . $this->theme .'/',
                                            'img_dir' => null,
                                            'js_dir' => null,
                                            'css_dir' => null,

                                    ));

    \Theme::instance()->set_template($this->template)->set_global('theme', $this->theme);

    In my view
    echo \Asset::instance($theme)->img('logo.jpg', array('alt' => 'My image');


    So i leave theme support for my shared theme assets, but i keep it for my app theme assets.

    PS :
    I attributes this values to generate Asset instance:
    'img_dir' => null,
     'js_dir' => null,
    'css_dir' => null,


    because i'm on windows and if i do no do, it no works because i have a backslash in my output path :
    http://fuelphp.shared.local:8080/themes/my_theme/img\logo.jpg
  • Which version of Fuel are you on?

    I think I saw a fix for that, you might want to swap your fuel/core for 1.8/develop, and see if that fixes it. If not, please create an issue for it at https://github.com/fuel/core/issues, with your config and a code snippet, so it can be looked at.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion