【问题标题】:Search all controllers in cakephp在 cakephp 中搜索所有控制器
【发布时间】:2014-09-16 11:23:55
【问题描述】:

我需要从一个表单中对任何控制器执行查询,我试试这个:

<select id="control">
        <option value="labs">Laboratórios</option>
        <option value="computers">Computadores</option>
        <option value="hists">Históricos</option>
    </select>
    <script>
        $(document).ready(function(){
            $("#control").change(function(){
                var value = $(this).val();
            });
        });
    </script>
    <?php 
        $controller = '<script>document.write(value);</script>';
        print_r($controller);
        $base_url = array('controller' => $controller, 'action' => 'index' );
        echo $this->Form->create("Filter",array('url' => $base_url, 'class' => 'filter'));
        echo $this->Form->input("search", array('label' => 'Pesquisa', 'placeholder' => "Pesquisa..."));
        echo $this->Form->submit("Pesquisar");
        echo $this->Html->link("Reset",$base_url);
        echo $this->Form->end(); 
    ?>

但是变量 $controller 有字符串“&lt;script&gt;document.write(value);&lt;/script&gt;”,如何解决这个问题?或者有其他方法来做到这一点......

【问题讨论】:

  • 我现在的理解是,你想根据 $("#control") 值调用控制器。这意味着 $("#control").value 是控制器名称。对吗?
  • 是的。 “labs”、“computers”和“hist”是控制器名称。
  • 您不能将动态 javascript 值添加到 php。但是你可以在html中改变它(输出),可能是在表单actions中。

标签: javascript forms cakephp controller


【解决方案1】:

尝试使用model 而不是controller -

<?php 
    $base_url = array('controller' => 'any_controller', 'action' => 'any_action' );
    echo $this->Form->create("Filter",array('url' => $base_url, 'class' => 'filter'));
    $models = array('Model1' => 'Model1', 'Model2' => 'Model2', 'Model3' => 'Model3');
    echo $this->Form->input("model", array('label' => 'Model', 'options' => $models));
    echo $this->Form->input("search", array('label' => 'Pesquisa', 'placeholder' => "Pesquisa..."));
    echo $this->Form->submit("Pesquisar");
    echo $this->Html->link("Reset",$base_url);
    echo $this->Form->end(); 
?>

在 `any_controller 的 any_action() 中,您将获得所有值。

function any_action() {
    $model = $this->request->data['Filter']['model'];
    //instantiate the model
    $modelInit = ClassRegistry::init($model);
    //build search conditions depending on the other data
    $condition = array(...);
    $result = $modelInit->find('all', $condition);
    //set the result and redirect to your page`
}

【讨论】:

  • 谢谢'sgt',你的建议奏效了。我已经创建了一个函数,只是传递表单的参数。
【解决方案2】:

您可以简单地在更改下拉菜单时创建表单操作。我认为您在这里不需要 PHP。

<select id="control">
    <option value="labs">Laboratórios</option>
    <option value="computers">Computadores</option>
    <option value="hists">Históricos</option>
</select>
<script>
    $(document).ready(function(){
        //This will change the action on change of dropdown value
        $("#control").change(function(){
            var hostname = window.location.hostname;
            var controller = $(this).val();
            $('#filterForm').attr('action', 'http://'+hostname+'/'+controller+'/index');
        });
    });
</script>
<?php         
    $base_url = array('controller' => $controller, 'action' => 'index' );
    echo $this->Form->create("Filter",array('url' => $base_url, 'class' => 'filter','id'=>'filterForm'));
    echo $this->Form->input("search", array('label' => 'Pesquisa', 'placeholder' => "Pesquisa..."));
    echo $this->Form->submit("Pesquisar");
    echo $this->Html->link("Reset",$base_url);
    echo $this->Form->end(); 
?>

希望这对你有用..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多