Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Extending base controller
  • I want to create a base controller where I want to set my template's header, menu and footer and then extend this controller in my other controller. If I make different controllers for different views, this works fine but if i put different action methods to call different views in a single controller, my index page works fine but all other pages do not. My code looks something like this:

    class Controller_Public extends Controller_Template
      {    
           public $template = 'layout/main';
               public function main_view()
           {
      $this->template->header = View::forge('layout/header');
      $this->template->menu = View::forge('layout/menu');
      $this->template->footer = View::forge('layout/footer');
           }   
    }

    class Controller_Site extends Controller_Public
    {            
           public function action_index()
           {
      parent::main_view();
      $this->template->title = "Something";
      $this->template->content = View::forge('content/index');
       }
        
           public function action_register()
                {
    parent::main_view();
    $this->template->title = "Something else";
    $this->template->content = View::forge('content/register');
           }
    }

    If I create separate controller to call my register page, it works fine. However, if I do as above, my index page loads fine but when I go to my register page, I just get listing of all my content and my styles and formatting are all gone.Further, after I go to register page, navigating back to my index page generates error. I am not sure if I made myself clear. I am a complete newbie. Please help. 
  • HarroHarro
    Accepted Answer
    First, if you make a base controller, it's best not to call it Controller_something, and put it in classes/controller. That will make it publicly accessible. It's better to either put it in classes, or in a folder of classes (and change the class name accordingly).

    The problem you describe has nothing to do with your controllers, your views are not ok, or your base_url is not ok.

    From your description my guess is that when you request "http://yoursite", your assets are prefixed with that, and it works. When you request "http://yoursite/register", your assets are prefixed with that, and obviously nothing will be found.

    Are you using the Asset class, or something hardcoded? Perhaps you can post the header part of your "content/register" view? Use http://bin.fuelphp.com for larger code blocks, it's easier to read and reply to,
  • Thank you for the tips. I am new not only to fuelphp but to mvc as well.

    My header view looks like this: http://bin.fuelphp.com/snippet/view/si

    I am not sure if this has got anything to do with my problem but when i run my project, my url is "http://mysite", when I navigate to my register page, my url is "http://mysite/site/register" and my styles are gone. Then when I navigate back to my index page, my url is "http://mysite/site/index" and my styles are again gone.
  • The problem in your views is as I suspected. You use

    <link href="themes/bootstrap/css/bootstrap.css" rel="stylesheet">
       
    which is a link relative to the CURRENT url (because of the missing leading slash).

    So on your homepage, it translates to http://mysite/themes/bootstrap/css/bootstrap.css, but on your register page it translates to http://mysite/site/register/themes/bootstrap/css/bootstrap.css, which obviously doesn't exist.

    If you want to use hard/hand-coded URL's in your views, use Uri::create(), to make sure they are always generated on the applications base_url.

  • Thank you so much for pointing it out. I just used

    <link href="<?php echo Uri::base();?>themes/bootstrap/css/bootstrap.css" rel="stylesheet"> 

    and it works all fine.
  • There is no technical difference between that and

    Uri::create('themes/bootstrap/css/bootstrap.css');

    which I find a bit cleaner. Uri::create() is also more feature rich, so it can generate url's with "fake" extensions if you configure that, it supports query string generation from array data, https links, etc.

    But in the end it's personal preference.
  • Thank you for clarifying it further! :)
  • Thank you for clarifying it further! :)

Howdy, Stranger!

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

In this Discussion