Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Where can I place store/run global functions/variables?
  • Hi,

    Like in the every framework, sooner or later we would like to add our own global variables or function or even the loaders like for the db.php :
     \Config::load('db', true);

    What is the best way to do the above job in the FuelPHP?

    Recently I was in need to load the db config globally so I have placed the  \Config::load('db', true); right in the app/views/bootstrap.php (because its always loaded) - so is it a good practise?
  • If you want to have a config file available "globally", you can add it to the "always_load" section of your config file. No need to add it to a bootstrap.
  • Thats nice. What about the other function like fe. isLogged() ?

    Regards
  • HarroHarro
    Accepted Answer
    You mean you want a custom function to be defined always? Preferrably you use a static class, otherwise define them in a separate file (like we do in fuel/core/base.php), and load that in your app/bootstrap.
  • So I was doing fine :-). Thank you Harro!

    Btw. Is it possible for the View_Smarty to use the app/classes/view, just like it does with the ViewModel?

    I would like to prepare the view data there and keeping my data in there would help clean up the controller.
  • View files go in app/views, Viewmodels go in app/classes/view. A view file is not class, so it doesn't go in the classes folder.

    The parser package extends the View class in a transparent way, so if you pass a smarty template name to a Viewmodel, it will work as with any view.
  • Yes, I have already tried to pass a smarty template name to the ViewModel like this:
    Response::forge(ViewModel::forge('main/index.tpl'));
    In the results, it does treats the index.tpl like a PHP file - no smarty is being used there.
  • You have a Viewmodel class that matches that request?
    Do you have the parser package loaded?
    Do you have configured the Parser package correctly for Smarty?

    Just checked the Viewmodel code, and all it does at the end is View('main/index.tpl'); So if that works, I don't see why a Viewmodel won't.
  • 1) Yeah, this is my app/classes/view/main/index.php: http://pastebin.com/raw.php?i=feFfkUBF, app/classes/controller/main.php: http://pastebin.com/raw.php?i=UZgxJnQa. My index.tpl template file is located under the app/views/main/index.tpl

    2) I think so, its located automatically via the config's autoloader:
            'packages'  => array(
                'parser',
            ),
    3) Yeah, when I use:
    return Response::forge(View_Smarty::forge('main/index.tpl');
    instead of the:
    return Response::forge(ViewModel::forge('main/index.tpl'));
    in the app/classes/controller/main.php then Smarty is working on the index.tpl and I can use its methods.


  • You should not use

    return Response::forge(View_Smarty::forge('main/index.tpl');

    you shouldl use

    return Response::forge(View::forge('main/index.tpl');

    This is what the Viewmodel does too. If that doesn't work, your parser config is wrong. Did you configure the parser at all? Because by default the .tpl extension is linked to the Dwoo parser, not the Smarty parser.

    So if you want to use .tpl for Smarty, you need to configure a different extension for Dwoo, and  .tpl for Smarty. So you should have a app/config/parser.php with:

    return array(

        // ------------------------------------------------------------------------
        // Register extensions to their parsers, either classname or array config
        // ------------------------------------------------------------------------
        'extensions' => array(
            'dwoo'      => array('class' => 'View_Dwoo', 'extension' => 'dwoo'),
            'smarty'      => array('class' => 'View_Smarty, 'extension' => 'tpl'),
        ),
    );
  • Okay, I've changed the return forge type.

    Actually I was editing
    the app/packages/parser/config/parser.php file instead of the
    app/config/parser.php but I think it does not matter.

    So I have
    created the config file under the app/config/parser.php with the config
    you gave and .tpl file is still treated as a php file (I can use
    <?php echo time(); ?> and its working, Smarty delmiters doesn't work at all).

    I'm loading the Smarty right in the View_Smarty array config:

        'View_Smarty'   => array(
            'include'        => APPPATH.'vendor'.DS.'Smarty'.DS.'Smarty.class.php',
            'auto_encode'   => true,
            'delimiters'    => array('left' => '{', 'right' => '}'),
            'environment'   => array(
                'compile_dir'       => APPPATH.'tmp'.DS.'Smarty'.DS.'templates_c'.DS,
                'config_dir'        => APPPATH.'tmp'.DS.'Smarty'.DS.'configs'.DS,
                'cache_dir'         => APPPATH.'cache'.DS.'Smarty'.DS,
                'plugins_dir'       => array(),
                'caching'           => false,
                'cache_lifetime'    => 0,
                'force_compile'     => false,
                'compile_check'     => true,
                'debugging'         => false,
                'autoload_filters'  => array(),
                'default_modifiers' => array(),
            ),
        ),

    I'm out of ideas.

    #edit

    Oh, it worked when I change the 'smarty' key to the 'tpl' key like this:

        'extensions' => array(
            'dwoo'        => array('class' => 'View_Dwoo', 'extension' => 'dwoo'),
            'tpl'    => array('class' => 'View_Smarty', 'extension' => 'tpl'),
        ),

    Can I access the controller which have called the app/classes/view/index.php just from the viewmodel file?
  • Never change anything in framework code, including the configuration defaults. It will cause you headaches when you want to upgrade to a newer release, and you have to figure out what you have changed.

    Put your changes in app/config, they will override the defaults configured in the core or (in this case) a package.

    It does indeed use the array index to match the extension, so I should have changed that too. My mistake, sorry.

    If you have a requirement to access a controller from the "outside", you have a design issue you need to solve, as that should never be needed. All data the Viewmodel needs to prepare the view should be passed to the Viewmodel instance by the controller (same is true for a standard View object).

    Creating these kind of class dependencies and tight coupling is bad practice.
  • Thank you.

    Yet I have a problem passing the variable data array from the controller to the view/viewmodel. While it was working for the View_Smarty forge like this:

    return Response::forge(View_Smarty::forge('main/index.tpl', array('myvariable' => $query_result)));

    it doesnt work no more for the ViewModel:

    return Response::forge(ViewModel::forge('main/index.tpl', array('myvariable' => $query_result)));

    causing the error:

    Method name must be a string.

    So the error says that the method is being passed there instead.. so is it possibile to pass my controller data array to the View/ViewModel?
  • There is no method to pass data to a Viewmodel while forging. This is because the associated View object (that actually is the container for the data) is not created which forging.

    The alternative is:

    $viewmodel = Viewmodel::forge('main/index.tpl')->set('myvariable', $query_result);
    return Response::forge($viewmodel);
  • Thank you so much Harro. I gotta love this framework!

Howdy, Stranger!

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

In this Discussion