Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Handling errors using Theme class
  • Hi,

    I have recently switched from using Controller_Template to Theme class.

    I'm not really sure how can I now handle the errors, like 404.

    Using Controller_Template, this was fairly easy since in the controller function I only had to return response object or assign it to the $this->template->content:

    return Response::forge( Presenter::forge( 'welcome/404' ), 404 );
    What about the Theme class? I got a themes/default/templates/errors/ directory which contains views for common errors.

    I was able to generate view like this (I'm using presenter to set a variable, like in the fresh fuel installation). However, this one doesn't set the 404 header.

    $theme = Fuel\Core\Theme::instance();
    $theme->get_template()->set( 'title', '404 - not found' );
    $theme->set_partial( 'content', $theme->presenter( 'errors/404', 'view', NULL, 'templates/errors/404' ) );

    How can I handle errors in my controllers properly? Is there a nice way I could immadiately throw 404 in certain moment, eg. by a simple function call show_404();

    Regards
    Lucas
  • Our theme engine does:

        /**
         * The 404 action for the application.
         *
         * @access  public
         * @parm  Exception $e
         * @return  Response
         */
        public function action_404($e = null)
        {
            theme()->set_template('templates/404')->set('error', $e, false);
            return Response::forge(theme(), 404);
        }

    theme() is just a global helper function to retrieve the used Theme instance.
  • Thanks a lot!

    Got another question regarding Theme class:

    Sometimes I have to add some Javascript on my template view, however I don't output the script in the middle of my HTML. For the cosmetics purposes I'd like to output the used Javascript at the bottom of the page (before </body> tag).

    To do that, I'm currently storing the raw Javascript code in a PHP variable and queue it to asset, then output render in my layout like this:

    [parital view]

    $usedJavascript = '<script>console.log("Some javascript");</script>';
    Theme::instance()->asset->js( $usedJavascript, array(), 'view', true );

    [layout]

    [..]
    <?= Theme::instance()->asset->render('view'); ?>
    </body>
    </html>

    Is this a good practice? Honestly I don't really like the way of storing the Javascript in a PHP variable.
    Can this be done a better way?
  • If it is hardcoded javascript, specific to that view, why not put it in the view itself?

    We do things like 

    theme()->asset->css([
        'plugins/bootstrap-datepicker/datepicker.css',
        'plugins/bootstrap-datepicker/datepicker3.css',
        'plugins/font-awesome/font-awesome.min.css',
        'plugins/jquery-tag-it/jquery.tagit.css',
        'animate.min.css',
        'style.min.css',
        'style-responsive.min.css',
    ], [], 'header');

    theme()->asset->css('theme/default.css', ['id' => 'theme'], 'header');

    in our templates or widgets (this can be in any partial that you load), and then in the theme templates we do

    echo theme()->asset->render('header');

    to load js and css (mainly) to load in the page header, and something similar for the stuff at the bottom of the page

    theme()->asset->js([
        'plugins/slimscroll/jquery.slimscroll.min.js',
        'plugins/jquery-cookie/jquery.cookie.js',
    ], [], 'footer');


    and then just before </body> we do

    echo theme()->asset->render('footer');

  • Yes, I do use exact the same technique. My post was regarding the raw Javascript in the view, not files to include.

    I was asking if it's a good practice to do that the way I do (store Javascript in the PHP var) or there is a better way to achieve this. 
  • HarroHarro
    Accepted Answer
    You can do the same with raw / harcoded javascript.

    So the answer is: imho No, I always have frontend code in the view, whether it is loaded as a seperate file, or hardcoded/embedded.

    <?php
    // javascript to get the progress event fired
    $js = <<<'NOWDOC'
        $('#progress-modal').on('shown.bs.modal', function (event) {
            event.preventDefault();

            // cut lots of js code

            }, 3000);
        });
    NOWDOC;

    theme()->asset->js($js, [], 'footer', true);

Howdy, Stranger!

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

In this Discussion