CakePHP passing values between controllers - cakephp

I want to pass values from one controller to another.
For example I have a Conference controller and I want to create a new Event.
I want to pass the Conference id to the event to make sure those two objects are associated.
I would like to store in the ivar $conference using beforeFilter method.
Here is my beforeFilter function in the Events controller
public function beforeFilter() {
parent::beforeFilter();
echo '1 ' + $this->request->id;
echo '2 ' + $this->request['id'];
echo $this->request->params['id'];
if(isset( $this->request->params['id'])){
$conference_id = $this->request->params['id'];
}
else{
echo "Id Doesn't Exist";
}
}
Whenever I change url to something like:
http://localhost:8888/cake/events/id/3
or
http://localhost:8888/cake/events/id:3
I am getting an error saying that id is not defined.
How should I proceed?

when you're passing data via url you can access it via
$this->passedArgs['variable_name'];
For example if your URL is:
http://localhost/events/id:7
Then you access that id with this line
$id = $this->passedArgs['id'];
When you access a controller function that accepts parameters through url, you can use those parameters just as any other variable, like for example say your url looks like this
http://localhost/events/getid/7
Then your controller function should look like:
public function getid($id = null){
// $id would take the value of 7
// then you can use the $id as you please just like any other variable
}

In the Conferences Controller
$this->Session->write('conference_id', $this->request->id); // or the variable that stores the conference ID
In the Events controller
$conferenceId = $this->Session->read('conference_id');
Of course, on top you need
public $components = array('Session');

Related

Cakephp 4.x Passing Variables to Email Template

In CakePHP 4.x I'm having some trouble with the mailer() function, specifically passing variables from my controller to a template.
I need a user to be able to receive an email with a list of items. I'm able to use ajax to send an array to my controller, but I can't get the controller to pass the variables to the email template. I'm getting the email but not the dynamic content and I just can't get it to work via setViewVars.
My controller function:
public function bar(): void
{
if( $this->request->is('ajax') ) {
$this->request->allowMethod('ajax');
$foo = $this->request->getQuery('data');
// data is a stringified array
$mailer = new Mailer("default");
$mailer->viewBuilder()->setTemplate('default','default');
$mailer
->setEmailFormat('html')
->setFrom(['email#email.com' => 'Comparaciones'])
->setTo('email#email.com')
->setSubject('About')
>setViewVars($foo)
->send();
// ->deliver() doesn't work either
}
}
My template:
// standard cakephp default template
$content = explode("\n", $content);
foreach ($content as $line) :
echo '<p> ' . $line . "</p>\n";
endforeach;
I'm probably tripping up with something simple, I just can't get the array into the template. Any help much appreciated!!!

How to pass array value from view to another view Laravel 5.7

I want to get the value from the array I've passed from the first view to another view.
I found this works by using route (not url), but I don't know how to get it without parameter in the controller.
Please help me within the right syntax.
This is the latest code I've tried
$id = $request->id;
if($request->has('download', 'id')){
$pdf = PDF::loadView('pdfview');
return $pdf->download('pdfview.pdf',['id'=>$id])->with('id',$id);}
This is my code *used to download pdf file from a view
Controller
public function pdfview(Request $request)
{
$items = DB::table("items")->get();
view()->share('items',$items);
$id = $request->only(['id']);
if($request->has('download', 'id')){
$pdf = PDF::loadView('pdfview');
return $pdf->download('pdfview.pdf', ['id'=>$id])->with('id', $id);
}
return view('pdfview', ['id'=>$id])->with('id', $id);
}
Route
Route::get('pdfview',array('as'=>'pdfview','uses'=>'MaatwebsiteDemoController#pdfview'));
View
Download PDF
I want to get the $employee->nip value
But it is still Undefined variable: id
Don't really have time to test it. But this thread looks promising.
https://laravel.io/forum/03-14-2016-basics-passing-info-from-one-view-to-another

call action from action in one controller cakephp like controller/action/action

i have a controller called "Movies" so the link is "localhost/Movies" and have some actions in this controller like "localhost/Movies/view/[id]" , "localhost/Movies/view/category/[id]" if i want to make a path by the category name and sub category name like "localhost/Movies/English/new" .
how can i do something like this in cakephp 2. my project now like "localhost/English/new" but i want to put Movies in this path, to make it more fixable, if i want to make a new category just add a column in my database .
thanks
If i understand, maybe by creating a custom route for your action:
In app/config/routes.php:
Router::connect('/movies/:category/:subcategory', array('controller'=>'movies','action' => 'index'));
And you can retrieve the value in your controller with:
echo $this->params['category'];
echo $this->params['subcategory'];
You can also read about it in the cookbook http://book.cakephp.org/1.3/view/945/Routes-Configuration
The default routing in CakePHP is as follows:
mydomain/controller/action/param1/param2/param3/param4/...
If you want to add filter options to an action, just add optional parameters to the action.
function index($category = null, $subcategory = null) {
if(isset($subcategory)){
//will execute if you pass both arguments
}else if(isset($category)){
//will execute if you pass one argument
}else{
//will execute if you pass no arguments
}
}
EDIT
In this case, $category is param1, and $subcategory is param2. The overloaded function can receive either 1, 2, or no arguments. If this is in, for example, the ObjectsController, all these are valid URLs:
localhost/objects/index/ //$category==null, $subcategory==null
localhost/objects/index/foods/ //$category=='foods', $subcategory==null
localhost/objects/index/foods/green/ //$category=='foods', $subcategory=='green'
This allows you to control many options in the same action.

send parameter url

I am sending the values to a controller doing this
echo $html->link('Do this? ',"/item/view/{$form->value('table.id')}");
on the controller end cake just pickups the id like this
function view($id = null){
....
}
Now in addition to table.id, I want to send another value called table2.source ...how would I do that so the controller also gets it
function view($id = null, $source=null) ...something along those lines but not sure how will they get to the controller in first place
Can you not just change the link url to something like this:
echo $html->link('Do this? ',
"/item/view/{$form->value('table.id')}/{$form->value('table2.source')}");
(After changing the view function to accept the second parameter...)

Cakephp: how to access to model "variables" (mapped from db) from the controller after a set?

Ok i have this controller:
class ExampleController extends AppController {
var $name = 'Example';
public function test_me () {
$this->Example->Create();
$this->Example->set( 'variable_from_db_1' => 'random_value_1',
'variable_from_db_2' => 'random_value_2' );
//here, how can i access to variable_from_db_1 and 2 in $this->Example?
//???? i've tried $this->data and $this->Example->data but nothing to do
}
}
Do you have some hints for me?
you can explore the data with:
debug( $this->Example );
the data is it's own array:
$this->Example->data['variable_from_db_1'];
I don't think you can do that.
You can assign your model data to a $this->data array like this:
$this->data['variable_from_db_1'] = $value;
$this->set('variable_from_db_1', $value);
So know you can access $this->data within the controller.
I think if you want to save data to your actual Model, you might have to implement the getter / setter method in your model...
In the related view you can access it like this:
echo $variable_from_db_1.'<br />';
echo $variable_from_db_2.'<br />';
In the controller call
debug($this->data);
I think you cannot do this in controller, normally set calls are moved to the end area of actions and all the operations on the such variables must be performed before 'set'ing it for later access in views.

Resources