Yii HTML按钮调用控制器/操作

Yii HTML按钮调用控制器/操作,yii,yii-events,Yii,Yii Events,我正在尝试在Yii中创建html按钮,单击按钮我想调用UserController.php和Actioncreate 我是这样做的, echo CHtml::button('Signup', array('button' => array('Users/create'))); ?> 但它给了我以下的错误 htmlspecialchars() expects parameter 1 to be string, array given 这是堆栈跟踪 D:\wamp\www\yii

我正在尝试在Yii中创建html按钮,单击按钮我想调用UserController.php和Actioncreate

我是这样做的,

 echo CHtml::button('Signup', array('button' => array('Users/create'))); ?>
但它给了我以下的错误

htmlspecialchars() expects parameter 1 to be string, array given 
这是堆栈跟踪

D:\wamp\www\yii\framework\web\helpers\CHtml.php(85)

73      */
74     public static $liveEvents = true;
75 
76     /**
77      * Encodes special characters into HTML entities.
78      * The {@link CApplication::charset application charset} will be used for encoding.
79      * @param string $text data to be encoded
80      * @return string the encoded data
81      * @see http://www.php.net/manual/en/function.htmlspecialchars.php
82      */
83     public static function encode($text)
84     {
85         return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
86     }
87 
88     /**
89      * Decodes special HTML entities back to the corresponding characters.
90      * This is the opposite of {@link encode()}.
91      * @param string $text data to be decoded
92      * @return string the decoded data
93      * @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
94      * @since 1.1.8
95      */
96     public static function decode($text)
97     {

Stack Trace
#0  
+
 D:\wamp\www\yii\framework\web\helpers\CHtml.php(85): htmlspecialchars(array("Users/create"), 3, "UTF-8")
#1  
+
 D:\wamp\www\yii\framework\web\helpers\CHtml.php(2216): CHtml::encode(array("Users/create"))
#2  
+
 D:\wamp\www\yii\framework\web\helpers\CHtml.php(140): CHtml::renderAttributes(array("button" => array("Users/create"), "name" => "yt1", "type" => "button", "value" => "Signup"))
#3  
+
 D:\wamp\www\yii\framework\web\helpers\CHtml.php(436): CHtml::tag("input", array("button" => array("Users/create"), "name" => "yt1", "type" => "button", "value" => "Signup"))
#4  
–
 D:\wamp\www\nurseweb\protected\views\site\login.php(50): CHtml::button("Signup", array("button" => array("Users/create")))

45         <?php echo $form->error($model,'rememberMe'); ?>
46     </div>
47 
48     <div class="row buttons">
49         <?php echo CHtml::submitButton('Login');  
50  echo CHtml::button('Signup', array('button' => array('Users/create'))); ?>
51 <?php $this->endWidget(); ?>
52 </div><!-- form -->

#5  
+
 D:\wamp\www\yii\framework\web\CBaseController.php(127): require("D:\wamp\www\nurseweb\protected\views\site\login.php")
#6  
+
 D:\wamp\www\yii\framework\web\CBaseController.php(96): CBaseController->renderInternal("D:\wamp\www\nurseweb\protected\views\site\login.php", array("model" => LoginForm), true)
#7  
+
 D:\wamp\www\yii\framework\web\CController.php(870): CBaseController->renderFile("D:\wamp\www\nurseweb\protected\views\site\login.php", array("model" => LoginForm), true)
#8  
+
 D:\wamp\www\yii\framework\web\CController.php(783): CController->renderPartial("login", array("model" => LoginForm), true)
#9  
–
 D:\wamp\www\nurseweb\protected\controllers\SiteController.php(98): CController->render("login", array("model" => LoginForm))

093             // validate user input and redirect to the previous page if valid
094             if($model->validate() && $model->login())
095                 $this->redirect(Yii::app()->user->returnUrl);
096         }
097         // display the login form
098         $this->render('login',array('model'=>$model));
099     }
100 
101     /**
102      * Logs out the current user and redirect to homepage.
103      */

#10     
+
 D:\wamp\www\yii\framework\web\actions\CInlineAction.php(50): SiteController->actionLogin()
#11     
+
 D:\wamp\www\yii\framework\web\CController.php(309): CInlineAction->runWithParams(array("r" => "site/login"))
#12     
+
 D:\wamp\www\yii\framework\web\CController.php(287): CController->runAction(CInlineAction)
#13     
+
 D:\wamp\www\yii\framework\web\CController.php(266): CController->runActionWithFilters(CInlineAction, array())
#14     
+
 D:\wamp\www\yii\framework\web\CWebApplication.php(283): CController->run("login")
#15     
+
 D:\wamp\www\yii\framework\web\CWebApplication.php(142): CWebApplication->runController("site/login")
#16     
+
 D:\wamp\www\yii\framework\base\CApplication.php(162): CWebApplication->processRequest()
#17     
–
 D:\wamp\www\nurseweb\index.php(13): CApplication->run()

08 defined('YII_DEBUG') or define('YII_DEBUG',true);
09 // specify how many levels of call stack should be shown in each log message
10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
11 
12 require_once($yii);
13 Yii::createWebApplication($config)->run();
D:\wamp\www\yii\framework\web\helpers\CHtml.php(85)
73      */
74公共静态$liveEvents=true;
75
76     /**
77*将特殊字符编码为HTML实体。
78*将使用{@link capapplication::charset application charset}进行编码。
79*@param string$要编码的文本数据
80*@返回编码数据的字符串
81*@见http://www.php.net/manual/en/function.htmlspecialchars.php
82      */
83公共静态函数编码($text)
84     {
85返回htmlspecialchars($text,ENT_引号,Yii::app()->charset);
86     }
87
88     /**
89*将特殊HTML实体解码回相应字符。
90*这与{@link encode()}相反。
91*@param string$要解码的文本数据
92*@返回解码数据的字符串
93*@见http://www.php.net/manual/en/function.htmlspecialchars-decode.php
94*@自1.1.8
95      */
96公共静态功能解码($text)
97     {
堆栈跟踪
#0  
+
D:\wamp\www\yii\framework\web\helpers\CHtml.php(85):htmlspecialchars(数组(“用户/创建”),3,“UTF-8”)
#1  
+
D:\wamp\www\yii\framework\web\helpers\CHtml.php(2216):CHtml::encode(数组(“用户/创建”))
#2  
+
D:\wamp\www\yii\framework\web\helpers\CHtml.php(140):CHtml::renderAttributes(数组(“按钮”=>array(“用户/创建”),“名称”=>“yt1”,“类型”=>“按钮”,“值”=>“注册”))
#3  
+
D:\wamp\www\yii\framework\web\helpers\CHtml.php(436):CHtml::tag(“输入”,数组(“按钮”=>数组(“用户/创建”),“名称”=>“yt1”,“键入”=>“按钮”,“值”=>“注册”))
#4  
–
D:\wamp\www\nurseweb\protected\views\site\login.php(50):CHtml::button(“注册”,数组(“button”=>array(“用户/创建”))
45
46
47
48
49
51
52
#5  
+
D:\wamp\www\yii\framework\web\CBaseController.php(127):require(“D:\wamp\www\nurseweb\protected\views\site\login.php”)
#6  
+
D:\wamp\www\yii\framework\web\CBaseController.php(96):CBaseController->renderInternal(“D:\wamp\www\nurseweb\protected\views\site\login.php”,数组(“model”=>LoginForm),true)
#7  
+
D:\wamp\www\yii\framework\web\CController.php(870):CBaseController->renderFile(“D:\wamp\www\nurseweb\protected\views\site\login.php”,数组(“model”=>LoginForm),true)
#8  
+
D:\wamp\www\yii\framework\web\CController.php(783):CController->renderPartial(“login”,array(“model”=>LoginForm),true)
#9  
–
D:\wamp\www\nurseweb\protected\controllers\SiteController.php(98):CController->render(“登录”,数组(“模型”=>LoginForm))
093//验证用户输入,如果有效,则重定向到上一页
094如果($model->validate()&&&$model->login())
095$this->redirect(Yii::app()->user->returnUrl);
096         }
097//显示登录表单
098$this->render('login',array('model'=>$model));
099     }
100
101     /**
102*注销当前用户并重定向到主页。
103      */
#10     
+
D:\wamp\www\yii\framework\web\actions\CInlineAction.php(50):SiteController->actionLogin()
#11     
+
D:\wamp\www\yii\framework\web\CController.php(309):CInlineAction->runWithParams(数组(“r”=>“站点/登录”))
#12     
+
D:\wamp\www\yii\framework\web\CController.php(287):CController->runAction(CInlineAction)
#13     
+
D:\wamp\www\yii\framework\web\CController.php(266):CController->runActionWithFilters(CInlineAction,array())
#14     
+
D:\wamp\www\yii\framework\web\CWebApplication.php(283):CController->run(“登录”)
#15     
+
D:\wamp\www\yii\framework\web\CWebApplication.php(142):CWebApplication->runController(“站点/登录”)
#16     
+
D:\wamp\www\yii\framework\base\capapplication.php(162):CWebApplication->processRequest()
#17     
–
D:\wamp\www\nurseweb\index.php(13):capapplication->run()
08已定义('yi_DEBUG')或定义('yi_DEBUG',true);
09//指定每个日志消息中应显示多少级别的调用堆栈
10已定义(“YII_跟踪水平”)或定义(“YII_跟踪水平”,3);
11
一次12美元(yii);
13 Yii::createWebApplication($config)->run();
有谁能告诉我这个错误的含义,并指导我找到解决方案吗?

CHtml button()方法实际上只是一个按钮,它接受的第二个参数应该是一个键/值对数组,而不是一个数组数组(如示例中所示),有关详细信息,请参阅。使用这种方法的一种方法是:

echo CHtml::button('myButton',array(
    'class' => 'buttonClass',
    'id'    => 'buttonId',
));
这将输出如下内容:

<input class="buttonClass" id="buttonId" name="yt1" type="button" value="myButton">

如果您想要一个按钮来提交表单,您可以使用该按钮来提交表单所包含的表单

如果您想要一个按钮将用户重定向到一个单独的页面而不提交任何内容,那么我认为最好的解决方案是使用带有锚链接的图像,或者在按下按钮后使用jQuery重定向用户

如果是这样的话,这里有一个问题和几个答案可能会帮助你: