3

I am very new to Laravel, and now I am on my first Laravel project. Now I need to provide REST api for mobile. I followed REST resource controller documentation on Laravel website. But when I call my REST api, it is not returning any value.

How to complete rest api in Laravel? I am using Laravel 5.

My REST API server code follows.

"route"

Route::resource('/users','user_accessController');

"controller"

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Response;
use Illuminate\Http\Request;

use App\User;

class user_accessController extends Controller {

    public function index()
    {
        return Response::json(array('name'=>'wai yan'));
    }
}

Client code:

"using curl"

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, 'http://laravel.bbc:8080/users');
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_exec($ch);

    $res = curl_close($ch);

What is wrong with my code? It is not returning any value.

7
  • Try using the full namespae for the route Route::resource('/users','App\Http\Controllers\user_accessController'); Commented May 21, 2015 at 6:56
  • 2
    BTW: you should be following naming convention. See how classes are named in Laravel. No class is named like yours. Commented May 21, 2015 at 7:34
  • I already followed convention . Same result . Commented May 21, 2015 at 7:44
  • 1
    what happens when you visit this URL from your browser? http://laravel.bbc:8080/users Commented May 21, 2015 at 7:49
  • It is returning this . {"name":"Wai Yan"} Commented May 21, 2015 at 7:56

2 Answers 2

1

Firstly you need to understand Laravel's naming convention.

StudlyCase for your controllers.

Use artisan command to generate resource controller

php artisan make:controller UserAccessController

Your route:

Route::resource('/users','UserAccessController');

"controller" - file name: UserAccessController.php

namespace App\Http\Controllers;

use Response;
//use App\Http\Controllers\Controller; no need for this both files are in same namespace

use App\User;

class UserAccessController extends Controller {

    /**
    * Display a listing of the resource.
    *
    * @return Response
    */
    public function index()
    {
        return response()->json(['name' => 'wai yan']);
    }


}

Am using Laravel 5 with same code above and this is the output when I used curl command:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://laravel.dev/users

enter image description here

Sign up to request clarification or add additional context in comments.

10 Comments

I added this line in php . curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json')); . But it is not working .
are you running this code from same laravel project?
No . I am running from different project . So to consume it from different project , what should I do please ?
If the different project is using PHP I do suggest you use GuzzleHttp for making requests as its has so many nice methods. guzzle.readthedocs.org/en/latest example you can do this: $client = new Client(); $response = $client->get('http://laravel.dev/users'); $json = $response->json(); The echo $json['name'] \\prints Wai yan
Thank you so much for suggestion . I got the chance to know the new thing . But how can I access using Curl ? Any idea please ?
|
0
$chatToken = "put your API KEY";

header("Content-type: text / html; charset = utf-8");

$option = array (
    "body" => "Test posting Messages"
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL,"https://api.chatwork.com/v2/rooms/roomid/messages");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($option));
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('X-ChatWorkToken:'. $chatToken));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.