Function Cakephp短代码动态调用函数和元素

Function Cakephp短代码动态调用函数和元素,function,cakephp,view,Function,Cakephp,View,我的帖子有正文,在这篇文章中我放了短代码,比如[gallery=2],现在我可以在我的文本库中找到2个参数,2个我想在我的视图中动态调用函数库,它调用元素库id=2,如下所示: app/view/posts/index.ctp findshortcode($posts[Post][Body]); // this function find short code and call his name like gallery(2) //my problem is : function gall

我的帖子有正文,在这篇文章中我放了短代码,比如[gallery=2],现在我可以在我的文本库中找到2个参数,2个我想在我的视图中动态调用函数库,它调用元素库id=2,如下所示:

app/view/posts/index.ctp

findshortcode($posts[Post][Body]); // this function find short code and call his name like gallery(2) 

//my problem is :

function gallery($id = null){
    $this->element('gallery', array('galleryid' => $id), array('plugin' => 'gallerys'));  
}

你所拥有的并没有太多的意义,你描述的方式也不是简单的或MVC方式。但我想我明白你的问题,我会向你解释你想要的方式

你需要做的是。。。从控制器内部执行所有逻辑。。。然后将所有完成的变量发送到视图以显示

class PostsController extends AppController {

var $shortCodeTypes = array('gallery');

function view($id){
    $post = $this->Post->find(null, $id);
    $post = $this->_insertShortCodes($post);
    $this->set(compact('post'));
}

function _insertShortCodes($post){

    foreach($this->shortCodeTypes as $shortCodeType){
        // Do logic to find shortcodes and insert data into post body
    }
    return $post
}

thaks很多,我认为你的答案是正确的,我在MVC方式上的编程逻辑有错误。