CakePHP 2.5 HttpSocket Display Response - php

This is my first time using this and im having issues. I have a HttpSocket->get which is working but i dont know how to display the results in my view
public function index() {
$HttpSocket = new HttpSocket();
// array query
$results = $HttpSocket->get('https://myurl.com', array('username' => 'myuser','password'=>'mypassword','cmd'=>'mycommands'));
//debug($results);
}
The response is there i can see it when i debug. please someone tell me how to show this in my view!!!
Thanks
Steve

CakePHP provide set() method to send variable value on view
$this->set("results", $results);
Controller method TestsController.php
public function index() {
$HttpSocket = new HttpSocket();
// array query
$results = $HttpSocket->get('https://myurl.com', array('username' => 'myuser','password'=>'mypassword','cmd'=>'mycommands'));
$this->set("results", $results);
//debug($results);
}
For instance your controller name TestsController then you will create file /view/Tests/index.ctp
view file index.ctp
print_r($results);

Related

Retrieve data from database in codeigniter

I am new to Codeigniter so I'm having some difficulties I want to retrieve data from the database and load it in the view but I couldn't figure out how.
Here is my model:
public function viewClientWaterwell(){
$userid = get_client()->userid;
$waterwells = $this->db->select('*')
->from($this->table)
->where('ww_client_id', $userid)
->get()->result_array();
return $waterwells;
}
Here is my clientController:
public function viewClient()
{
$data['waterwells'] = $this->waterwell_model->viewClientWaterwell();
$this->data($data);
$this->view('waterwells/clients/viewClient');
$this->layout();
}
I want to pass some data in a view to the client side but I can't figure out how. I think there is some problem with my model but I cannot figure out what. I'm new to this and will appreciate your feedback. Thanks!
You just need to use second argument to pass data to your template or view file
Here is example to start
Inside your controller function
public function viewClient()
{
$data = array( 'myvar' => 'I love stackoverflow' );
$this->load->view('waterwells/clients/viewClient', $data);
}
And inside your template or view file you can access them by their name.
<?php echo $myvar; ?>

How to use variables in routes in laravel?

I'm trying to build a application in laravel 5.3 in which I get the variable from request method and then trying to pass that variable in a redirect to the routes. I want to use this variable in my view so that I can be able to display the value of variable. I'm currently doing this:
In my controller I'm getting the request like this:
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member' => $member);
}
Now I've following in my routes:
Route::get('/member/memberinfo', 'MemberController#memberinfo')->with('member', $member);
Now in MemberController I want to use $member variable and display this into my view:
public function memberinfo()
{
return view('member.memberinfo', ['member' => $member]);
}
But I'm getting an error in the routes files
Call to undefined method Illuminate\Routing\Route::with()
Help me out, how can I achieve this.
When you're using redirect()->with(), you're saving data to the session. So to get data from the session in controller or even view you can use session() helper:
$member = session('member'); // In controller.
{{ session('member')['xyz'] }} // In view.
Alternatively, you could pass variables as string parameters.
Redirect:
return redirect('member/memberinfo/xyz/abc')
Route:
Route::get('/member/memberinfo/{xyz}/{abc}', 'MemberController#memberinfo');
Controller:
public function memberinfo($xyz, $abc)
{
return view('member.memberinfo', compact('xyz', 'abc'));
}
You can use like this:
route:
Route::get('/member/memberinfo', 'MemberController#memberinfo')
and the redirect:
return redirect('member/memberinfo')->with('member', $member);
You need to replace => with ,
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member', $member); // => needs to be replaced with ,
}
Hope this works!
Replace line
return redirect('member/memberinfo')->with('member' => $member);
to
return redirect('member/memberinfo')->with('member', $member);
......

cakephp how to send data from controller to view with json

Hi I'm trying to make an API to send data encapsulating with json.
as cakephp manual said, I added extensions in routes.php
$routes->extensions(['json]);
and I've made an index function in controller.
public function index(){
$item = $this->Items->find('all');
$this->set(['items' => $items, '_serialize' => ['items']]);
}
here are the problem.
what should i do after this to make api encapsulating with json??
Please help.
thank you
According to Cake 2.x Book (http://book.cakephp.org/2.0/en/development/rest.html)
You have to add this to your routes.php file:
Router::mapResources('items');
Router::parseExtensions();
Then, in your items controller, add the RequestHandler to your components array:
public $components = array('RequestHandler');
Then, in your items controller, add your methods, in your example:
public function index() {
$recipes = $this->Items->find('all');
$this->set(array(
'items' => $items,
'_serialize' => array('items')
));
}
Note: according to model names convention you should call $this->Item instead of $this->Items unless you previously defined the model name as "Item" (singular) in your item model file.
Finally, the API is done, you can access to yourprojecturl/items.json and see the json result.
I had trouble with RicardoCamacho's code until I used the $recipes, which is $this->Items->find('all'); in the $this->set(array...
public function index() {
$recipes = $this->Items->find('all');
$this->set(array(
'items' => $recipes,
'_serialize' => array('items')
));
}

Return a JSON Array to a View in Laravel

I am using Laravel 4to build an applicaiton.
This is my code:
public function handleCreateCandle(){
$candle = new Candle;
$candle->name = Input::get('name');
$candle->body = Input::get('body');
$candle->user_id = Sentry::getUser()->id;
$candle->save();
return Redirect::action('User_BaseController#getPrayerSpace')->with('message', 'Thank you for lighting candle');
}
The issue is rather than returning a message I would like to return a json array to the view with a success code and the name and body of $candle. Can anyone advise me on how to do this?
Instead of using save() function you can use create() function to create a new row.
Create() function returns you the last inserted row, which you can use to show it in your message.
Example:
$user = User::create(array('name' => 'John'));
Please refer:
http://laravel.com/docs/eloquent

cakephp load component on fly

I want to load component on fly in cakePHP
In this example, loading dynamically RequestHandler component for json response
public function xyz() {
$this->components[] ='RequestHandler';
$this->Components->load('RequestHandler');
$this->RequestHandler->initialize($this);
$abc = array("hello","world");
$this->set('abc', $abc);
$this->set('_serialize', array( 'abc'));
}
error generated on initialize function shows undefined.
AMENDMENT FOR MORE CLEAR PICTURE:
public function xyz() {
$this->components[] ='RequestHandler';
$this->RequestHandler = $this->Components->load('RequestHandler');
$this->RequestHandler->initialize($this);
$abc = array("hello","world");
$this->set('abc', $abc);
$this->set('_serialize', array( 'abc'));
}
I also tried
public function xyz() {
$this->RequestHandler = $this->Components->load('RequestHandler');
$abc = array("hello","world");
$this->set('abc', $abc);
$this->set('_serialize', array( 'abc'));
}
I can't use component like this #$this->RequestHandler->getTime(); because cakephp automatic handle json respone.
When I hit just above code using http://disecake.localhost/resources/xyz.json
{"code":500,"url":"/resources/xyz.json","name":"View file "
/var/www/disecake/app/View/Resources/xyz.ctp" is missing."}
When I use
public $components = array( 'RequestHandler');
in my cotroller than output
{"abc":["hello","world"]}
I think now question is more clear.
There is literally a section in the CakePHP book called "Loading Components on the fly".
CakePHP 2.x:
http://book.cakephp.org/2.0/en/controllers/components.html#loading-components-on-the-fly
CakePHP 3.x:
http://book.cakephp.org/3.0/en/controllers/components.html#loading-components-on-the-fly
(and it's done different than how you're attempting)

Categories