在 PHP 中以 MVC 模式干净地设置局部变量
Setting local variables cleanly in a MVC pattern in PHP
我正在尝试使用 MVC 模式在某些代码中设置局部变量,即
$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>";
当然,控制器调用模型并执行所有后端工作,解析输入、获取数据然后返回。
我想要的是在 setController
中实例化的 seoModel
中设置本地 $page_title 等变量的干净方法,而不使用 $_SESSION 或任何其他 hacky有点办法。
是否可以从设计 POV 将方法放入控制器中以获取信息?即
$page_title = seoController->getPageTitle();
我的控制器目前还没有以这种方式使用,因为它们所做的只是将我的模型连接到视图。
我希望我的解释足够清楚。
Is it ok from a design POV to put methods in the controller to get the info?
是的,这就是 Controller
的意思。
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.
为了避免使用 $_SESSION
对于这种特殊情况似乎有点矫枉过正,您可以设置 seoController
属性,例如,
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
}
}
并从调用方访问它们
$controller = new seoController;
$controller->getPageTitle();
$page_title = $controller->page_title;
您通常会将诸如元标记之类的内容与其描述的模型一起存储。因此,如果您要加载某个模型的产品,那么该模型也可能 return 该产品的元标记:
public function show($productId)
{
$product = $this->productModel->findById($productId);
// Meta title may be available at $product->meta_title
return new ViewModel(array(
'product' => $product,
));
}
然后您的控制器操作将 return 需要在视图中显示的数据,可以是 HTML 模板、JSON、XML 等
我正在尝试使用 MVC 模式在某些代码中设置局部变量,即
$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>";
当然,控制器调用模型并执行所有后端工作,解析输入、获取数据然后返回。
我想要的是在 setController
中实例化的 seoModel
中设置本地 $page_title 等变量的干净方法,而不使用 $_SESSION 或任何其他 hacky有点办法。
是否可以从设计 POV 将方法放入控制器中以获取信息?即
$page_title = seoController->getPageTitle();
我的控制器目前还没有以这种方式使用,因为它们所做的只是将我的模型连接到视图。
我希望我的解释足够清楚。
Is it ok from a design POV to put methods in the controller to get the info?
是的,这就是 Controller
的意思。
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.
为了避免使用 $_SESSION
对于这种特殊情况似乎有点矫枉过正,您可以设置 seoController
属性,例如,
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
}
}
并从调用方访问它们
$controller = new seoController;
$controller->getPageTitle();
$page_title = $controller->page_title;
您通常会将诸如元标记之类的内容与其描述的模型一起存储。因此,如果您要加载某个模型的产品,那么该模型也可能 return 该产品的元标记:
public function show($productId)
{
$product = $this->productModel->findById($productId);
// Meta title may be available at $product->meta_title
return new ViewModel(array(
'product' => $product,
));
}
然后您的控制器操作将 return 需要在视图中显示的数据,可以是 HTML 模板、JSON、XML 等