Adding GET input to my resourceful controller in Laravel - php

Using Laravel 4 to create a "Read-it-Later" application just for testing purposes.
I'm able to successfully store a URL and Description into my application using the following curl command:
curl -d 'url=http://testsite.com&description=For Testing' readitlater.local/api/v1/url
I'm interested in using GET to accomplish the same thing but by passing my variables in a URL (e.g. readitlater.local/api/v1/url?url=testsite.com?description=For%20Testing)
Here is my UrlController segment:
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
$url = new Url;
$url->url = Request::get('url');
$url->description = Request::get('description');
$url->save();
return Response::json(array(
'error' => false,
'urls' => $urls->toArray()),
200
);
}
Here is my Url model:
<?php
class Url extends Eloquent {
protected $table = 'urls';
}
I read through the Laravel docs on input types but I'm not certain how to apply that to my current controller: http://laravel.com/docs/requests#basic-input
Any tips?

You didn't apply what you correctly linked to...Use Input::get() to fetch anything from GET or POST, and the Request class to get info on the current request. Are you looking for something like this?
public function store()
{
$url = new Url; // I guess this is your Model
$url->url = Request::url();
$url->description = Input::get('description');
$url->save();
return Response::json(array(
'error' => false,
'urls' => Url::find($url->id)->toArray(),
/* Not sure about this. You want info for the current url?
(you already have them...no need to query the DB) or you want ALL the urls?
In this case, use Url::all()->toArray()
*/
200
);
}

Related

Laravel Resource controller

How do I tell my API to display a particular result based on another column?
e.g. localhost:8000/api/gadgets/{{id}}
Normally it returns the particular information of the specific gadget with that ID and localhost:8000/api/gadgets/{{imei_code}} does not return any value or an error whereas imei_code is a column that I needed to pass as a GET request...
I'm using the normal resource controller
public function show(Gadgets $gadget)
{
$response = ['data' => new GadgetResource($gadget), 'message' => 'specific gadget'];
return response($response, 200);
}
Also I need help on how I can create like a search function in the controller.
You can`t do two similar URLs. I think your route for URL
localhost:8000/api/gadgets/{{imei_code}}
isn`t work. Also the order of the routes is important and route that defined firstly will be have higer priority then route that defined secondly.
Because your routes /api/gadgets/{{id}} and /api/gadgets/{{imei_code}} is similar in this case only the one described earlier will be processed.
You can define another router and handler, for example:
localhost:8000/api/gadgets
That will return a list of gadgets by default and you can add filters for imei_code. For example:
localhost:8000/api/gadgets?imei_code=123
And your handler for the new route may be writed something like that:
public function showList(Request $request): GadgetResource
{
if ($imeiCode = $request->query('imei_code')) {
$list = Gadget::query()->where('imei_code', $imeiCode)->get();
} else {
$list = Gadget::query()->take(10)->get();
}
return GadgetResource::collection($list);
}
Or like alternative solution you can create diferent route for searching of gadgets exactly by imei_code to get rid of any route conflicts
localhost:8000/api/gadgets/by_imei/123
public function findByImei(Request $request): GadgetResource
{
$imeiCode = $request->route('imei_code');
$item = Gadget::query()->where('imei_code', $imeiCode)->first();
return new GadgetResource($item);
}
You can specify the model key by scoping - check docs
Route::resource('gadgets', GadgetController::class)->scoped([
'gadget' => 'imei_code'
]);
Than, when Laravel try to bind Gadget model in Controller - model will will be searched by key imei_code.
This code equvalent of
Route::get('/gadget/{gadget:imei_code}');
Try to change response
public function show(Gadgets $gadget)
{
$response = ['data' => new GadgetResource($gadget), 'message' => 'specific gadget'];
return response()->json($response);
}

Wrapping JSON response in an abstracted way in PHP / Laravel

I am making a REST API that will return different JSON responses depending on which type of User is making the call.
There is a single endpoint: example.com/api/v1/collect that uses Laravel's API authentication to get the User model with $user = auth()->guard('api')->user();.
Each User will belong to a Type.
If User 1 (type_id 1) makes the call, the response will look like:
{
"example": 1,
"success": true,
"data" : [
...
]
}
If User 2 (type_id 2) makes the call, the response can be different, depending on the user's type. It could look like:
{
"example": 2,
"response" : [
...
],
"custom": "string",
"success": 200
}
The ... is the data that we are sending back (for example a list of Post titles) and it will always be the same, but the "envelope" (or wrapper) around it would be specific to each user (or type of user).
So far, I've found two solutions to wrap that ... in an abstracted way:
Solution 1: Using Laravel Blade
// Api\V1\ApiController.php
$data = $user->posts->pluck('title');
// Each type of user will have a different blade filename
// There could be around a 100 types which will result in a 100 blade files
// The filename is stored in the database
$filename = $user->type->filename; // returns 'customBladeTemplate'
// Return a JSON response after passing the $data to the view
return response()->json([
view($filename, compact('data'))->render(),
]);
Using a blade file for each type of user allows me to wrap the data like this:
// resources/views/customBladeTemplate.blade.php
// This filename has to match the one in the database column
{
"example": 1,
"success": true,
"data" : [
{!! $data !!}
]
}
That will output a JSON response for the User 1 (example 1)
Solution 2: Using Laravel response macros
// Api\V1\ApiController.php
$data = $user->posts->pluck('title');
// Each type of user will have a different macro name
// There could be around a 100 types which will result in a 100 different macros
// The macro name is stored in the database
$macroName = $user->type->macro_name; // returns 'customMacroName'
return response()->{macroName}($data);
Creating a Macro for each type of user, using the macro name from the DB:
// App\Providers\AppServiceProvider.php
use Illuminate\Http\Response;
public function boot()
{
Response::macro('customMacroName', function ($data) {
return Response::json([
'example' => 2,
'response' => $data,
'custom' => 'string',
'success' => 200,
]);
});
}
That macro will output a JSON response for the User 2 (example 2)
Both options work fine but I am still wondering:
Is there another (possibly better) way to do it?
Are those two solutions valid or can they be enhanced?
Which of those two solutions seem to be better and why?
Edit: The $data is not actually coming from an eloquent model, it is rather from a serialized JSON column (JSON casting) - which means I can't use the Laravel API resources
If you are looking for the response formatting you should go with the Laravel API Resources
Based on your requirement(data formate different for two type of users), you can create two different Api Resource classes.
AdminResource & UserResource.
Here you have more flixibility on controlling fields or orgnizing data.
Here is how you can define the resource class:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class UserResource extends Resource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request
* #return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
And you can use this as:
use App\User;
use App\Http\Resources\UserResource;
Route::get('/user', function () {
return new UserResource(User::find(1));
});
If you would like to include with condition check in with user type, you can create a common function called renderJson($userType, $data) and place this in your parent class or can wrap with traits, all depends on your application architecture.
Here you can find laravel documentation for API Resource: https://laravel.com/docs/5.8/eloquent-resources
Edited:
With Laravel API Resource, you not only parse the modal object, you can parse any arrayble object.
Essentially they are just simple objects with one very important job
to do — transform your objects (interesting I said objects and not
models). To do this out of the box, all you have to do is instantiate
the Resource (collection or individual) with an Arrayable object. If
you did nothing else but generate a standard Resource and pass in an
Arrayable object the Resource would transform that object
automatically, and because Models are Arrayable this is where I got
caught out because if you create a resource collection and instantiate
it with a collection of models then the models get toArray'd and not
their corresponding resource.
Src: https://medium.com/#dinotedesco/laravel-api-resources-what-if-you-want-to-manipulate-your-models-before-transformation-8982846ad22c
So in your case if you can just collect() the json data and pass to api resource.
You could use middlewares to change what the response looks like.
With middleware you could change the response after the execution of your regular code, without having to take this into account in the controller itself. Using the below code you modify the response AFTER it has been executed.
<?php
namespace App\Http\Middleware;
use Closure;
class AfterMiddleware
{
public function handle($request, Closure $next)
{
// Calls the controller and processes the request.
$response = $next($request);
// Here you can retrieve the user and modify the response however you want.
// Some example code:
$user = Auth::user();
if ($user->type == 1) {
... //Change response for user type 1
}
if ($user->type == 2) {
... //Change response for user type 2
}
// Etc...
return $response;
}
}
Reference: https://laravel.com/docs/5.8/middleware
Depends on how different the responses are from each other. I'm inclined to take inventory of each type's common features and build a response array as appropriate. This could be done in the controller or a helper function and then returned using Laravel's JSON response type.
$response = [];
// results common to all types
$response['example'] = $example;
$response['success'] = $success;
// customized results for specific types
if (in_array($type, [1, 3, 4, 5, ...])) {
$response['data'] = $dotdotdot;
}
if (in_array($type, [2, 6, 7, 8, ...])) {
$response['response'] = $dotdotdot;
$response['custom'] = $custom;
}
return response()->json($response);
I don't know if this is what you are looking for. I had something similiar a few months ago and fixed it with json files. As json is amazingly fast and you can create thousands of types.
Sorry for my bad english i will fix it after the weekend :-)
Let's get started.
First the user logs in using laravel passport or api routes.
Second the api calls a controller.(class). I will create a class based on your info.
let's say the api calls the ApiController and the method handle
use Illuminate\Http\Request;
class ApiController
{
public function __construct()
{
}
/**
* Handle the incoming request
*
* #param Request $request
*/
public function handle(Request $request)
{
//first let's find the correct format
$type = $requets->user()->type; //lets say type_1
$config = $this->getUserType($type);
//i don't know where you data comes from but let's say $data is your data.
$data = json_encode(['some' => "data", 'to' => "return"]);
//lets fill the data
$response = $this->fillDataInConfig($data, $config);
return response()->json($response);
}
/**
* Find the user type config by type name
*
* #param string $type
* #return object
*/
private function getUserType(string $type) : string
{
//let store them in the local storage
$config = \Storage::disk('local')->get("api_types/$type.json");
return json_decode($config);
}
/**
* Fill the data
*
* #param mixed $data
* #param object $config
* #return object
*/
private function fillDataInConfig($data, object $config) : object
{
//as for your example. The reusl//
// {
// "example": 2,
// "response" : *responseData*, <===== where the response should be
// "custom": "string",
// "success": 200
// }
foreach($config as $index => $value){
if($value === '*responseData*'){
$config->{$idnex} = $data;
}
}
//the data is filled in the response index
return $config;
}
}

How to fill Request object in command line REPL tool Tinker with emulated data of real http request

Using Laravel framework and it's REPL named Tinker in my project, I want to set the request object to the same state it would be if I made some real HTTP request through my browser.
When I dump the request using
dd($request);
I receive a lot of data in $request like headers, form input data, and so on.
I want to receive the same data in $request in Tinker REPL.
How can I emulate HTTP request in Tinker from the command line?
You should be able to instantiate a request object and then use replace to get some input data into it. Something like this should work in tinker...
>> $r = new Illuminate\Foundation\Http\FormRequest()
>> $r->replace(['yada' => 'bla bla bla'])
>> $r->yada
That should output bla bla bla.
Request class has some set of methods to initiate it with names begins from create... And the create method allow to initiate it with manually gived params like url, method and additional optional parameters:
Illuminate\Foundation\Http\FormRequest::create('http://your-url', 'METHOD', ...)
so you can use it from REPL to play with your controllers and initiate them like you come from some route
I know this question is pretty old but I recently wanted to test a controller POST and found it difficult to find an answer, so here is my solution:
I created a formRequest class which I wanted to test in Tinker. The class looks something like this:
<?php
namespace App\Http\Requests;
use App\Models\Task;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
class TaskRequest extends FormRequest
{
private $_validator;
private $_task;
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
// public function authorize()
// {
// return false;
// }
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'task_name' => ['required', 'max:100'],
'task_description' => ['nullable', 'max:1024'],
'due_date' => ['nullable', 'max:50','after:today'],
// ... and more
];
}
public function validator()
{
return $this->_validator ?? $this->_validator = Validator::make($this->sanitize(), $this->rules());
}
/**
* Convert any of your incoming variables, such as dates to required format
**/
public function sanitize()
{
// get request vars
$vars = $this->all();
if ( !empty($vars['due_date']) )
{
$date = new Carbon($vars['due_date'] );
$vars['due_date'] = $date->toDateTimeString();
}
return $vars;
}
public function save()
{
if ( $this->validator()->fails() )
return false;
return $this->_task = Task::create(
$this->validator()->validated()
);
}
In tinker:
$form = new App\Http\Requests\TaskRequest();
$form->merge(['task_name'=>'test task','due_date'=>'2022-04-22Z15:45:13UTC']);
$form->validator()->validated();
=> [
"task_name" => "test task",
"due_date" => "2022-04-22 15:45:13"
]

Why is my dynamic route returning 200 on only one ocassion?

I have a route that calls a third-party API and returns its response. This route can take an ID as a parameter like so: /my/route/{id}.
When I request /my/route/1 I get a 200 success, but when I do /my/route/2, /my/route/3, /my/route/4, etc I get 500 error.
The funny thing is that I always get the correct response body. So both the 200 and 500 responses are returning the data that I need.
My issue is when getSponsor(..) is triggered here:
<?php
namespace App\Http\Controllers\Matrix;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class SponsorReadingController extends Controller
{
/**
* SponsorReadingController constructor.
*/
public function __construct()
{
$this->cookieJar = app('MatrixCookieJar');
$this->client = app('GuzzleClientForMatrix');
}
public function getSponsor($sponsorId, Request $request)
{
// TODO: Refactor this monstrous function
if (!AuthController::isAlreadyLoggedIn($request)) {
$loginRes = AuthController::loginToMatrixApi($this->cookieJar);
if ($loginRes->getStatusCode() === 200) {
$sessionId = AuthController::getSessionIdFromResponse($loginRes);
} else {
return $loginRes;
}
} else {
$sessionId = null;
AuthController::setAuthCookie(
$this->cookieJar, $request->cookie('matrix_api_session')
);
}
$respData = [
'error' => null,
'message' => null,
'data' => json_decode(
$this->client->get(
'/admin/sponsor/detail',
[
'query' => ['sponsorId' => $sponsorId],
'cookies' => $this->cookieJar,
'allow_redirects' => false
]
)->getBody())->response
];
return $this->handleResponse($respData, $sessionId);
}
/**
* Handle the response with the provided data and
* cookie value.
*
* #param array $respData
* #param string $cookieVal
* #return Response
*/
public function handleResponse($respData, $cookieVal)
{
if (!empty($cookieVal)) {
return response()->json($respData)->withCookie(
cookie('matrix_api_session', $cookieVal, 29, '/matrix/api')
);
}
return response()->json($respData);
}
EDIT: If I do dd($res) instead of return $res inside handleResponse(...) I get a 200 status code, weird.
For reference, this answer helped me out: 500 Internal Server Error for php file not for html
So basically, I added ini_set('display_errors', 1) so the response would include any errors on the back-end that I wasn't seeing (turned out apache's error log had it), and sure enough there was an error that was not directly affecting the response so I would still get the correct response data.
The file were the cookies were being stored couldn't be seen from Guzzle's point of view, but the Laravel app itself could see it. For obvious reasons you need to specify the full path to a folder/file on the server. I ended up using Laravel's own storage folder to store them, so I had to change my service provider from this (where cookies/jar.json used to be in Laravel's public folder:
$this->app->bind('MatrixCookieJar', function ($app) {
return new FileCookieJar(
'cookies/jar.json', true
);
});
To this:
$this->app->singleton('MatrixCookieJar', function ($app) {
return new FileCookieJar(
storage_path('cookies').'/'.'jar.json', true
);
});

Laravel retrieving data from REST API

Okay so I have a following situation:
The system I am building is retrieving data from a REST api and saving that data into a database. What I am wondering is how could this be implemented and where would behaviour like this go in sense of Laravels structure (controller, model etc.)? Does Laravel have a built in mechanism to retrieve data from external sources?
Edit:
Buzz hasn't been updated for over a year, it's recomended to now use Guzzle, see Mohammed Safeer's answer.
I have used Buzz package in order to make API requests.
You can add this package by adding it to the require section in your composer.json file.
{
require: {
"kriswallsmith/buzz": "dev-master"
}
}
Then run composer update to get it installed.
Then in Laravel you can wrap it in a class (perhaps a repository-like class) that handles making API request and returning data for your app to use.
<?php namespace My\App\Service;
class SomeApi {
public function __construct($buzz)
{
$this->client = $buzz;
}
public function getAllWidgets()
{
$data = $this->client->get('http://api.example.com/all.json');
// Do things with data, etc etc
}
}
Note: This is pseudocode. You'll need to create a class that works for your needs, and do any fancy dependency injection or code architecture that you want/need.
As #Netbulae pointed out, a Repository might help you. The article he linked is a great place to start. The only difference between the article and what your code will do is that instead of using an Eloquent model to get your data from your database, you're making an API request and transforming the result into a set of arrays/objects that your application can use (Essentially, just the data storage is different, which is one of the benefits of bothering with a repository class in the first place).
We can use package Guzzle in Laravel, it is a PHP HTTP client to send HTTP requests.
You can install Guzzle through composer
composer require guzzlehttp/guzzle:~6.0
Or you can specify Guzzle as a dependency in your project's existing composer.json
{
"require": {
"guzzlehttp/guzzle": "~6.0"
}
}
Example code in laravel 5 using Guzzle as shown below,
use GuzzleHttp\Client;
class yourController extends Controller {
public function saveApiData()
{
$client = new Client();
$res = $client->request('POST', 'https://url_to_the_api', [
'form_params' => [
'client_id' => 'test_id',
'secret' => 'test_secret',
]
]);
$result= $res->getBody();
dd($result);
}
First you have to make routes in your app/routes.php
/*
API Routes
*/
Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
Route::resource('pages', 'PagesController', array('only' => array('index', 'store', 'show', 'update', 'destroy')));
Route::resource('users', 'UsersController');
});
Note: If you are not required authentication for API call, you can remove 'before' => 'auth.basic'
Here you can access index, store, show, update and destroy methods from your PagesController.
And the request urls will be,
GET http://localhost/project/api/v1/pages // this will call index function
POST http://localhost/project/api/v1/pages // this will call store function
GET http://localhost/project/api/v1/pages/1 // this will call show method with 1 as arg
PUT http://localhost/project/api/v1/pages/1 // this will call update with 1 as arg
DELETE http://localhost/project/api/v1/pages/1 // this will call destroy with 1 as arg
The command line CURL request will be like this (here the username and password are admin) and assumes that you have .htaccess file to remove index.php from url,
curl --user admin:admin localhost/project/api/v1/pages
curl --user admin:admin -d 'title=sample&slug=abc' localhost/project/api/v1/pages
curl --user admin:admin localhost/project/api/v1/pages/2
curl -i -X PUT --user admin:admin -d 'title=Updated Title' localhost/project/api/v1/pages/2
curl -i -X DELETE --user admin:admin localhost/project/api/v1/pages/1
Next, you have two controllers named PagesController.php and UsersController.php in your app/controllers folder.
The PagesController.php,
<?php
class PagesController extends BaseController {
/**
* Display a listing of the resource.
*
* #return Response
* curl --user admin:admin localhost/project/api/v1/pages
*/
public function index() {
$pages = Page::all();;
return Response::json(array(
'status' => 'success',
'pages' => $pages->toArray()),
200
);
}
/**
* Store a newly created resource in storage.
*
* #return Response
* curl --user admin:admin -d 'title=sample&slug=abc' localhost/project/api/v1/pages
*/
public function store() {
// add some validation also
$input = Input::all();
$page = new Page;
if ( $input['title'] ) {
$page->title =$input['title'];
}
if ( $input['slug'] ) {
$page->slug =$input['slug'];
}
$page->save();
return Response::json(array(
'error' => false,
'pages' => $page->toArray()),
200
);
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
* curl --user admin:admin localhost/project/api/v1/pages/2
*/
public function show($id) {
$page = Page::where('id', $id)
->take(1)
->get();
return Response::json(array(
'status' => 'success',
'pages' => $page->toArray()),
200
);
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
* curl -i -X PUT --user admin:admin -d 'title=Updated Title' localhost/project/api/v1/pages/2
*/
public function update($id) {
$input = Input::all();
$page = Page::find($id);
if ( $input['title'] ) {
$page->title =$input['title'];
}
if ( $input['slug'] ) {
$page->slug =$input['slug'];
}
$page->save();
return Response::json(array(
'error' => false,
'message' => 'Page Updated'),
200
);
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
* curl -i -X DELETE --user admin:admin localhost/project/api/v1/pages/1
*/
public function destroy($id) {
$page = Page::find($id);
$page->delete();
return Response::json(array(
'error' => false,
'message' => 'Page Deleted'),
200
);
}
}
Then you have model named Page which will use table named pages.
<?php
class Page extends Eloquent {
}
You can use Laravel4 Generators to create these resources using php artisan generator command. Read here.
So using this route grouping you can use the same application to make API request and as a front-end.
You can choose what to use:
Guzzle
CURL
file_get_contents :
$json = json_decode(file_get_contents('http://host.com/api/v1/users/1'), true);
Referrer
Try looking into the external API's manuals. There you will find info on how to retrieve information.
Then the best plan is to build an Interface.
Check this out:
http://culttt.com/2013/07/08/creating-flexible-controllers-in-laravel-4-using-repositories/
It's up to you how you use php to solve this.

Categories