Reputation: 360
Is that possible to get variable from other function in same Controller ?
So I just updated my code ... the huge code is my real code ... so I wish to get the $hashfilename_filename
to another function so I able to save it into DB
Example:
class HappyController extends Controller{
public function actionUploadFile()
{
if (isset($_FILES['Filedata']['tmp_name']) && is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
$today = date("Ymd");
$slash = Yii::app()->params['slash'];
$tmp_folder = Yii::app()->params['tmp_folder'];
$tmp_folder_with_index_file = $tmp_folder . $slash . 'index.html';
$tmp_folder_with_date = Yii::app()->params['tmp_folder'] . $today;
if (!is_dir($tmp_folder_with_date)){
mkdir($tmp_folder_with_date, 0755);
copy($tmp_folder_with_index_file, $tmp_folder_with_date . $slash . 'index.html');
}
$filesize = sprintf("%u", filesize( $_FILES['Filedata']['tmp_name'] ));
$hashfilename_filename = md5(time() + 1) . '.apk';
$full_path = $tmp_folder_with_date . $slash . $hashfilename_filename;
if (!move_uploaded_file ($_FILES['Filedata']['tmp_name'], $full_path)){
$result['statusCode'] = "500";
echo json_encode($result);
die();
}
$result['statusCode'] = "200";
$result['today'] = $today;
$result['tmp_folder_with_date'] = $tmp_folder_with_date;
$result['filesize'] = $filesize;
$result['hashfilename_filename'] = $hashfilename_filename;
$result['full_path'] = $full_path;
}else{
$result['statusCode'] = "400";
}
echo json_encode($result);
die();
}
public function actionLife(){
$model = new ThisisLife();
$model->sad = $hashfilename_filename;
$model->save();
}
}
In public function actionLife , I wish to get the variable from other function, any suggestion to do that ?
Upvotes: 1
Views: 1566
Reputation: 67
If you need to access variable through controllers, why don't u make it a private field in controller. So that you can access it in whole Controller class. You then may have getter, setters if needed, as it should as we are talking about OOP.
Upvotes: 0
Reputation: 1296
What you are trying to do is not the right way in my opinion. The idea behind OOP i to encapsulate code belonging together. So if you need to determine a path which is needed in more than one place (or action) just extract it into its own private function within the controller. That way you could call this method from both actions and reuse your code.
If you need this variable between two calls I'd rather pass it as a GET/POST-Parameter as the otherway around you risk using the same filename again if you forget to reset the var...as it says, it lasts the whole session!
Your method could look like this:
private function generatePath()
{
$folder = Yii::app()->params['tmp_folder'] . date("Ymd");
$folderWithIndex = Yii::app()->params['tmp_folder'] . DIRECTORY_SEPARATOR . 'index.html';
if (!file_exists($folder)) {
mkdir($folder, 0755);
copy($folderWidthIndex, $folder . DIRECTORY_SEPARATOR . 'index.html');
}
$filename = md5(time() + 1) . '.apk';
return $folder . DIRECTORY_SEPARATOR . $filename;
}
The constant DIRECTORY_SEPARATOR is a php default constant to automatically fill in the "slash" of the current filesystem. One more input: Instead of defining the path in your params, you could set it as a yii-alias. This makes life much easier in the long run. Make sure to check it out here: https://github.com/yiisoft/yii2/blob/master/docs/guide/concept-aliases.md
I hope it helped!
cheers, pascal
Upvotes: 0
Reputation: 21
try storing it in a session variable;
public function actionAbc(){
$full_path = a + b;
Yii::app()->user->setState('full_path', $full_path);
}
public function actionXyz(){
$full_path = Yii::app()->user->getState('full_path');
}
In this way you can access this variable from anywhere across whole platform.
Upvotes: 2