CakePHP - Pass parameter to controller (NOT url request) - php

There is a function in AppController:
function products()
{
$products=$this->Product->find('all',array('order' => array('Product.publish_order ASC') ,'fields'=>array('id','name','price','publish')));
$this->set('products',$products);
$counter=$this->Pincode->find('count',array('order' => array('Pincode.product_id DESC '),'conditions' => array('product_id' => $pid,'status'=>0)));
$this->set('counter',$counter);
}
In my layout products.ctp I need to set a $pid (as you see in appcontroller class method I used this) and pass it through.

Another solution is to use the cakephp session
Code to add Value in session :
$this->Session->write('Product.id', 25);
Code to read Value from session :
$pid= $this->Session->read('Product.id');
Code to delete Value from session :
$this->Session->delete('Product.id');

Related

How to get a segment URL cakephp 3

Hi I got trouble in retrieve URL segment CAkephp3 in view. I want to get the ID from current URL.
Lets say my URL is http://localhost/admin/financial_agreements/edit/50
and I want redirect to http://localhost/admin/financial_agreements/do_print/50
simply :
var urlPrint = "<?=$this->Url->build(['controller' => 'financial_agreements', 'action' => 'do_print', 'I NEED ID FROM CURRENT'], true)?>";
I try debug
<?=debug($this->Url->build()); die();?>
But its produce : admin/financial_agreements/edit/50
whats called in 50 ? I need that 50 inside my "url->build" urlPrint
sorry for bad english.
Anyhelp will appreciate.
thanks.
You can use the Request object to get request data (including url parameters) within views.
Try this in your view:
$this->request->getParam('pass') //CakePHP 3.4+
$this->request->params['pass'] // CakePHP 3.3
That will return an array of all non-named parameters that were passed after the action's name in the URL. Example: /mycontroller/myaction/param1/param2. So in this example, $this->request->getParam('pass') will produce an array like: [0 => 'param1', 1 => 'param2'].
Bonus answer: you can also 'name' parameters in the URL, like: /mycontroller/myaction/some_name:some_value. To retrieve this kind of named parameters, you would do the same trick but using: $this->request->getParam('named') (Use the argument 'named' instead of 'pass').
More info:
https://book.cakephp.org/3.0/en/controllers/request-response.html
https://book.cakephp.org/3.0/en/development/routing.html#passed-arguments
Assuming that your edit function follows standard practices, you'll have something like this:
public function edit($id) {
$financialAgreement = $this->FinancialAgreements->get($id);
...
$this->set(compact('financialAgreement'));
}
Then in edit.ctp, you can get the id of the current record very simply as $financialAgreement->id, so your URL will be generated with
$this->Url->build(['controller' => 'financial_agreements', 'action' => 'do_print', $financialAgreement->id], true)

Yii can't access session variables

My problem: I can't access session data in the view, which I tried to save in the controller before rendering the view.
In my opinion there's an error when storing the session data. (It's necessary for me to modify the session data after creating it, not only in the single action.)
ProcessController.php
public function actionNew() {
$formhash = md5(time());
$hashList = array();
$hashList[$formhash]['processingNumber'] = '';
//fill with empty model
$hashList[$formhash]['model'] = $this->loadModel();
//store hashList in session
Yii::app()->session['hashList'] = $hashList;
$this->render('/process', array('hashValue => $formHash));
}
Now in the view I need the data from the session to show them to the user. But when dumping the hashList it just dumps "null" (Maybe because the saving in the controller didn't went well).
process.php
<?php
$form = this->beginWidget('CActiveForm', array(
'id' => 'process_form',
//several other things...
));
//Output: null
CVarDumper::dump(Yii::app()->session['hashList'],10,true);
?>
I tried to use $_SESSION instead of Yii::app()->session, which gives me access to the data in the view. But when handling other actions in the Controller the $_SESSION variable is undefined.
Any suggestions?
Thank you.
Long answer is:
Regarding to this documents:
$session=new CHttpSession;
$session->open();
$value1=$session['name1']; // get session variable 'name1'
$value2=$session['name2']; // get session variable 'name2'
foreach($session as $name=>$value) // traverse all session variables
$session['name3']=$value3; // set session variable 'name3'
You can use as well:
Yii::app()->session->set('hashList', $hashList);
Yii::app()->session->get('hashList');
And set it again.
Beside this session thing, why do not you use this:
$this->render('/process', array('hashValue => $formHash, 'hashList' => $hashList));
So you do not need to save it in a session if you can reach it directly into the view.
According to the documentation your code should work. An alternative might be to use the following, but it does the same:
Yii::app()->session->add('hashList', $hashList); // set the value
$hashList = Yii::app()->session->get('hashList'); // get the value
I expect the problem either a debugging problem, meaning you have observed cached or otherwise outdated data or a problem in parts of your code that you have not shown.

CodeIgniter adding session variable to an already defined "named session"

I already created as session named "verified" as shown below (in my login controller):
foreach($result as $row)
{
$sess_array = array(
'id' => $row->memberid, //Session field: id.
'username' => $row->member_userunique //Session field: username.
);
// Create a session with a name verified, with the content from the array above.
$this->session->set_userdata('verified', $sess_array);
}
Now, when a user open a page called book (with a controller named book) i want to add one more additional variable called "book_id" to my "verified" session.
This is what i have done
function index()
{
//Add a new varible called book_id
$this->session->set_userdata('book_id', $titleID);
}
When i tried to retrieve the 'book_id' using the following method:
$session_data = $this->session->userdata('verified');
$article_id = $session_data['book_id'];
$user_id = $session_data['id'];
It only able to retrieve the 'id' however the 'book_id' is not defined. But if do a var_dump() on $this->session->all_userdata() i can see that the 'book_id' session variable has been successfully appended.
After reading the CI documentation about session, i realized that the code above will not work as i have not tell it to which session should i add the variable to.
Can anyone point me to the right direction?
You do as follows in your index() method:
$session_data = $this->session->userdata('verified');
$session_data['book_id'] = "something";
$this->session->set_userdata("verified", $session_data);
This way you retrieve the contents of the variable (i.e. the array that you persisted earlier), you add another key to the array (book_id), and then store it again. Now you will be able to do, as I assume you want to:
$sess = $this->session->userdata("verified");
echo $sess['book_id'];

Codeigniter: Accessing a array in view

I am completely new in the codigniter i just started day befire Yesterday i am having a problem
Here is my snippet of my controller code
$allcalldetails_array = array(
'id' => $row->id,
'customer_id' => $row->customer_id
);
$this->session->set_userdata('logged',$allcalldetails_array);
I want to iterate the $allcalldetails_array in my views please tell me the way to do this
i tried iterating logged but could not get anything .
If i am printing the array in my views like print_r($allcalldetails_array); but this is also disappointing me .Please help me to get back on track .
Thanks
Its not necessary for session variables to be parsed via controller, access it on view directly:
$logged = $this->session->userdata('logged');
print_r($logged);
If you would like data to be send to your view as variables, you should add the corresponding data to your view load.
Controller:
$logged = $this->session->userdata('logged');
$this->load->view('viewfile', $logged);
View
print "logged id:".$id;
best regards.
Jonas
To get the data from session your need to do the following:
// controller logic
$logged = $data['logged_for_view'] = $this->session->userdata('logged'); // since 'logged' is the name you set it to
// more controller logic
$this->load->view('view_name', $data);
// in view
var_dump($logged_for_view); // this is the key of the $data variable you assigned for the view
Regarding the CodeIgniter's documentation :
Data is passed from the controller to the view by way of an array or
an object in the second parameter of the view loading function.
Then you could use something like :
// Controller side
$data['allcalldetails_array'] = array(
'id' => $row->id,
'customer_id' => $row->customer_id
);
$this->load->view('your_view', $data);
// View side
print_r($allcalldetails_array);
// Loop through your array
foreach ($allcalldetails_array as $detail){
// Do something with your $detail.
}

sending data from model to controller in codeigniter 1.7.2

I am receiving $fb_data[uid] i.e Facebook UID from my model and I want to put this variable's value into my controller where the signup process is taking place
passing fields in controller like this:
$sql_data = array('student_fname' => $student_fname, 'student_sname' => $student_sname, 'student_org' => $student_org, 'student_title' => $student_title, 'student_uid' => ?
I am confused how to insert the Facebook UID value here at my controller..
You need to have a function in your model which returns your $fb_data. In the controller you have (if not already done) to load the model
$this->load->model('model_name');
Then you can access your model function from the controller by calling
$this->model_name->function_name();
The section 'loading a model' in the CI documentation explains it again.
Do like this :
First create one method in controller :
Ex : In controller
function fbcontent() {
$this->load->model('your model name');
$content = $this->model_name->fbaction();
}
and In model
function fbaction(){
$sql_data = array('student_fname' => $student_fname, 'student_sname' => $student_sname, 'student_org' => $student_org, 'student_title' => $student_title, 'student_uid' => ?
return $sql_data;
}

Categories