skip to Main Content

I am experimenting with using the MVC pattern to set local vars in some code ie

$action=basename(__FILE__, '.php');               // load action from filename for consistancy (index for this case)
$controller = new seoController($action . '-seo'); // register controller with page action and parameter
$controller->invoke();                            // invoke controller for processing

$page_title = "<insert page title here>";
$page_desc = "<insert page desc here>";
$page_keys = "<insert page keywords here>";

Of course the controller calls the model and does all the backend stuff parsing the input, getting the data and then returning.

What I would like is a clean way to set the local $page_title etc vars from the seoModel that is instantiated in setController without using the $_SESSION or any other hacky kind of way.

Is it ok from a design POV to put methods in the controller to get the info? ie

$page_title = seoController->getPageTitle();

My controllers as of now are not being used in this type of way as all they do is connect my models to the views.

I hope I’m being clear enough with my explanation.

2

Answers


  1. Is it ok from a design POV to put methods in the controller to get the info?

    Yes, thats what Controller is meant for.

    What I would like is a clean way to set the local $page_title etc vars from the seoModel that is instantiated in setController without using the $_SESSION or any other hacky kind of way.

    To avoid using $_SESSION that seems to be a bit overkill for this particular case you can set seoController attributes, for example,

    Class seoController
    {
        $public $page_tile = '';
    
        public method getPageTitle()
        {
            $model = new seoModel();
            $page_title = $model->get_page_title();
            $this->page_tile = $page_title;
            //you could also return the page title here, skipping that  
        }
    }
    

    And access them from the caller

    $controller = new seoController;
    $controller->getPageTitle();
    $page_title = $controller->page_title;
    
    Login or Signup to reply.
  2. You would normally have things like meta tags stored with the model it’s describing. So if you’re loading say, a product from a model, then that model may also return the meta tags for that product:

    public function show($productId)
    {
        $product = $this->productModel->findById($productId);
    
        // Meta title may be available at $product->meta_title
    
        return new ViewModel(array(
            'product' => $product,
        ));
    }
    

    Your controller action would then return the data needed to be displayed in a view, which could be a HTML template, JSON, XML etc.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search