This is probably more of a pure ‘PHP’ question but what I am trying to do is clone a controller file and then just change the name of the file and the class. Similar to what gii does but without the template approach. I just want to clone an existing file and then change the class name.
I have tried a few ways to read in the file. This is the first few lines of the actual file that I want to read/clone.
<?php
class SBCmsController extends Controller
{
/**
* Declares class-based actions.
*/
public function init()
{
$this->layout = '//layouts/column1';
$this->defaultAction = 'home.html';
}
public function actions()
{
Pretty basic stuff with the exception of an unusual default action.
This is the code that reads the file and echos the contents.
$file_handle = fopen($controllerPath.DIRECTORY_SEPARATOR.$sourceController, "r");
$num =0;
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $num.$line;
$num++;
}
fclose($file_handle);
die;
It seems like it should work and just for debugging I added the line # ($num), this is my output.
It is very strange that the beginning of the file is completely missing. It’s like PHP or Yii(?) is doing something because it is a class.
Any ideas?
I have tried a few other methods with the same result.
// $sourceFile = Yii::app()->file->set($controllerPath.DIRECTORY_SEPARATOR.$sourceController);
// $str=implode("\n",file($controllerPath.DIRECTORY_SEPARATOR.$sourceController));
I have tried ‘file’ and the ‘Cfile’ extension found in the extensions repository.
‘Cfile’ uses file_get_contents() internally.
Pretty stumped on this one.
doodle
Just an update, it is not a Yii problem it is PHP.
I used this code outside of Yii and got the same result.
<?php
echo '<pre>';
$file_handle = fopen('SBCmsController.php', "r");
$num =0;
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $num.$line;
$num++;
}
fclose($file_handle);
?>