CGridView列按钮语法错误:意外的'"'求助
Let's break down the issue you're facing: that unexpected quote error is happening because of messy quote nesting in your value attribute, plus incorrect handling of the $data["id"] variable. Your original code tries to splice the variable into the URL string but ends up creating invalid PHP syntax.
What Went Wrong
In your original value string, you wrapped $data["id"] in single quotes, which means PHP treats it as a literal string instead of a variable. On top of that, the way you nested quotes for the onclick handler creates a syntax conflict—PHP can't parse the mixed quotes correctly. Also, directly appending parameters to the URL path isn't the Yii way to build URLs, which adds unnecessary complexity.
Solution 1: Fix the String Splicing (Legacy Approach)
If you need to stick with the string-based value definition, correct the quote escaping and variable handling like this:
array( 'header' => "Action", 'id' => 'id', 'name' => 'name', 'type' => 'raw', 'value' => 'CHtml::button("Settings", array( "onclick" => "document.location.href=\'" . Yii::app()->createUrl(\'admin/settings/AddEditPaymentSetting/edit\', array(\'action\' => \'edit\', \'eid\' => $data[\'id\'])) . "\'", "class" => "btn btn-default btn-top-rv btn-block" ))', 'htmlOptions' => array( 'style' => 'text-align: center;' ), )
Here, we:
- Properly escape single quotes inside the outer single-quoted string
- Pass
$data['id']as a parameter tocreateUrl()instead of splicing it into the URL path - Use Yii's recommended URL building pattern with parameter arrays
Solution 2: Use an Anonymous Function (Recommended)
If you're using Yii 1.1.14 or later, anonymous functions for value are supported and way easier to read—no more quote escaping headaches:
array( 'header' => "Action", 'id' => 'id', 'name' => 'name', 'type' => 'raw', 'value' => function($data) { return CHtml::button("Settings", array( "onclick" => "document.location.href='" . Yii::app()->createUrl('admin/settings/AddEditPaymentSetting/edit', array('action' => 'edit', 'eid' => $data['id'])) . "'", "class" => "btn btn-default btn-top-rv btn-block" )); }, 'htmlOptions' => array( 'style' => 'text-align: center;' ), )
This approach lets you write clean PHP code directly, so you don't have to juggle nested quotes. The $data variable is passed directly to the function, making variable access straightforward.
Key Notes
- Always use
Yii::app()->createUrl()with parameter arrays instead of hardcoding URL paths—it handles URL management rules automatically and avoids errors. - Anonymous functions make column configuration much more maintainable, especially for complex actions.
内容的提问来源于stack exchange,提问作者Hoodini

