dongya8378 2015-04-20 17:45
浏览 41
已采纳

在PHP中以MVC模式干净地设置局部变量

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条回答 默认 最新

  • dongxuan1314 2015-04-21 13:08
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序