gpt4 book ai didi

php - 为什么我会收到“"Class ' Yii' not found”错误?

转载 作者:行者123 更新时间:2023-11-30 00:11:28 25 4
gpt4 key购买 nike

使用 YII,我尝试从 ajax 调用的页面进行查询

echo CHtml::ajaxLink(
'Test request',
array('ajax/z1_gerar_excel.php',
'type'=>'POST',
'data' => array('sql' => $sql)
),
array('update'=>'#req_res'
)
);
?>

<div id="req_res">...</div>

这是 AJAX 页面:

$sql = $_REQUEST['data']['sql'];
//echo $sql;
$connection=Yii::app()->db;
$command = $connection->createCommand($sql);
$dataReader = $command->query();
$cont_l = 1;
while(($row = $dataReader->read()) !== false)
{
echo "<br>Linha $cont_l: ";
print_r($row);
$cont_l++;
}// WHILE EOF

$connection->active = false;

我有以下问题,YII类没有在ajax调用的页面中初始化,所以我得到以下结果:

Fatal error: Class 'Yii' not found in D:\Programas\Xampp\Instalado2\htdocs\atayo-teste\ajax\z1_gerar_excel.php on line 4

我只使用 YII 一星期。如何解决此错误?

更新:

所以我尝试将其更改为更 YII 的方式,我按照 http://lostmahbles.com/simple-yii-ajaxlink/ 中的示例进行操作并将其添加到我的 View (z1_sql.php):

echo CHtml::ajaxLink(
"Link Text",
Yii::app()->createUrl( 'application/controllers/RelatorioController/ajaxRequest' ),
array( // ajaxOptions
'type' => 'POST',
'beforeSend' => "function( request )
{
// Set up any pre-sending stuff like initializing progress indicators
}",
'success' => "function( data )
{
// handle return data
alert( data );
}",
'data' => array( 'val1' => '1', 'val2' => '2' )
),
array( //htmlOptions
'href' => Yii::app()->createUrl( 'application/controllers/RelatorioController/ajaxRequest' ),
'class' => 'actionAjaxRequest'
)
);

并将其发送到我的 Controller (RelatorioController.php):

public function accessRules()
{
return array(
array('allow',
'actions'=>array('ajaxrequest'),
'users'=>array('@'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}

public function actionAjaxRequest()
{
$val1 = $_POST['val1'];
$val2 = $_POST['val2'];

echo "some sort of response";

Yii::app()->end();
}

但是我收到了 404 错误。再说一次,对于 YII 来说真的是菜鸟,所以任何事情都有帮助......

最佳答案

您没有请求 Yii php 函数/页面。将您的代码更改为如下所示:(未经测试)

<?php
echo ajaxLink('text', array('ajax/z1_gerar_excel'), array(
'type' =>'POST',
'data' => array('sql' => $sql),
), array(
'update'=>'#req_res' // this array is for htmlOptions, so I think you want this parameter in the array with the ajax options
));
?>

确保您有一个具有函数 z1_gerar_excel 的 ajax Controller 。

有关 ajaxLink 参数的更多信息,请查看: http://www.yiiframework.com/doc/api/1.1/CHtml#ajaxLink-detail

关于php - 为什么我会收到“"Class ' Yii' not found”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24003395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com