Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中以MVC模式干净地设置局部变量_Php_Design Patterns_Model View Controller - Fatal编程技术网

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

在PHP中以MVC模式干净地设置局部变量,php,design-patterns,model-view-controller,Php,Design Patterns,Model View Controller,我正在试验使用MVC模式在一些代码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 $

我正在试验使用MVC模式在一些代码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>";
到目前为止,我的控制器没有以这种方式使用,因为它们所做的只是将我的模型连接到视图

我希望我的解释足够清楚

从设计角度来看,将方法放入控制器中以获取信息可以吗

是的,
Controller
就是这个意思

我想要的是一种干净的方法来设置seoModel中的本地$page\u title等变量,该seoModel在setController中实例化,而无需使用$\u会话或任何其他黑客方式

为了避免在这种情况下使用
$\u 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;

您通常会将元标记之类的东西存储在它所描述的模型中。因此,如果您正在加载某个模型中的某个产品,那么该模型也可能会返回该产品的元标记:

public function show($productId)
{
    $product = $this->productModel->findById($productId);

    // Meta title may be available at $product->meta_title

    return new ViewModel(array(
        'product' => $product,
    ));
}

然后,您的控制器操作将返回需要在视图中显示的数据,这些数据可能是HTML模板、JSON、XML等。

也非常有用……我喜欢这种风格……希望我能将这两个答案都标记为正确:)
public function show($productId)
{
    $product = $this->productModel->findById($productId);

    // Meta title may be available at $product->meta_title

    return new ViewModel(array(
        'product' => $product,
    ));
}