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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化