Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Presenter and different layouts
  • Hi all, and Happy New Year.
    I have a question about Presenter class. I want to use some templates with Presenter.
    For example:

    Controller class:

    <?php

    namespace Admin;

    use Fuel\Core\Controller;
    use Fuel\Core\Presenter;
    use Fuel\Core\Response;

    class Controller_Auth extends Controller {

        public function action_login()
        {
            return Response::forge(Presenter::forge('auth/login'));
        }
    }

    ______________________________

    Presenter class:

    <?php

    namespace Admin;

    use Fuel\Core\Presenter;

    class Presenter_Auth_Login extends Presenter {

        public function view()
        {

        }
    }

    ______________________________

    I don't know how use header, footer template for this view.
  • I did it. See code below

    <?php

    namespace Admin;

    use Fuel\Core\Controller;
    use Fuel\Core\Presenter;
    use Fuel\Core\Response;
    use Fuel\Core\View;

    class Controller_Auth extends Controller {

        public function action_login()
        {
            return Response::forge(Presenter::forge('auth/login')
                ->set('header', View::forge('template/header'))
                ->set('footer', View::forge('template/footer'))
            );
        }
    }

    If you know another method, write him please.
  • HarroHarro
    Accepted Answer
    Assuming the footer and header are the same on all pages, I would use a base controller for that, that extends Controller_Template

    So you'll get

    class Controller_Base extends Controller_Template {}

    And

    class Controller_Auth extends Controller_Base {}

    your base controller will setup the template, deal with header and footer and all other code that repeats, and keep your Controller_Auth clean and tidy.

    Oh, and remove all those use statements. NEVER use classes from \Fuel\Core !
  • Thank!

    why
    I should not use \Fuel\Core? What's problem? I use modules, and I have an error without this statement.
  • HarroHarro
    Accepted Answer
    All Fuel classes are aliased to the global namespace, which is part of the way Fuel extensions work. If you reference the core class directly, you bypass extensions, which make cause your app to break. Some packages no longer work properly, also, all core classes load other core classes from the global namespace, so you might end up with two distinct copies of the same class active.

    So when you use Fuel classes, you need to load the from the global namespace, either by prefixing them with a \:

    return \Response::forge( ... );

    or by "using" them from global:

    use Response;
    return Response::forge( ... );

  • Great!!! Thanks a lot. I'm newibe. I never work with FuelPHP, and i like it! So I have more question.
    How can I use global style (template) into my modules? For example:

    <?php

    namespace Admin;

    class Controller_Auth extends \Controller_Template {

        public $template = 'template/admin';

        public function action_login()
        {
            $this->template->title = 'Login';
            $this->template->content = \Response::forge(\Presenter::forge('auth/login'));
        }
    }

    ___

    public $template = 'template/admin'; <- FuelPHP takes local template from module, I wanna take module from app/views
  • HarroHarro
    Accepted Answer
    Fuel's filesystem is "cascading". It will search the local scope first (in this case the module), and if not found, it will fall back (to app in this case).

    You can not specify you want to load from the app, if you want that you have to make sure there is no view name collision.

    We solve that by using reserved "prefixes", for example all "templates/..." and "global/..." views are only defined in the app, and not in any module.
  • Wonderful! It's very clear and logical framework!

Howdy, Stranger!

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

In this Discussion