I am using Laravel and by the way new to the framework, i want to integrate a payment gateway that requires a callback url,
Route::post('qp', 'App\Http\Controllers\Api\MainAPIController#qp');
This is how i defined my route in the api.php and web.php. But unfortunately i cannot find my url which should be https://myApp.com/api/qp. What did i do wrongly.
actually below is my callback function. Am using laravel
$data = json_decode(file_get_contents('php://input'));
$user=$data->user_id;
$amounts=$data->amount;
$transaction_source=$data->funds_source;
$transaction_source=$data->funds_source;
$transaction_status=$data->status;
$reference=$data->client_ref;
$transaction_date=$data->trans_date;
$narration=$data->naration;
$charges=$data->charges;
if($transaction_status=="SUCCESS"){
}
elseif($transaction_status=="FAILED"){
}
Related
I am creating APIs for an app. Now app developer wants me to create a fixed base url and pass the ROUTE NAME (Which will point to controller function) as POST variable. Example:
http://example.com/Api
and POST variables like:
action=>'ROUTE_NAME'
But in laravel we can define the routes based upon the url parts as:
http://example.com/Api/ROUTE_NAME
I have tried using a single controller and loading the other controllers based upon SWITCH statements. But that doesn't seem to be a standard practice as i need to add switch condition every time I'll create a new API. Also middleware will not work on the loaded controllers dynamically.
Is there a way in laravel to achieve this? I am using laravel 5.4
You could implement a middleware that listens on the /Api route, which gets the ROUTE_NAME from the $request, then you could use the Route() helper function to find the url of that named route, then redirect the request to that route.
Something like:
// Generating ROUTE_NAME url...
$url = route($request->route_name);
// Redirect to that route...
return redirect()->route($url);
Obviously you'll need to add code to handle if it doesn't find a route etc, maybe return a json response back with a proper error code etc.
I got this error-->'NotFoundHttpException in RouteCollection.php line 161'..When i try to call my additional controller in laravel 5.2..Already I did php artisan serve to activate localhost:8000..can you please explain the basic layout of routing with controller in laravel?
NotFoundHttpException occurs when no given route is matched to your given request to a certain endpoint/url.
Make sure you are sending the request to the correct url which is correctly defined in your routes.php (web.php for laravel 5.3+) with it's correct verb, (GET, POST, PATCH, etc).
Basic flow goes like this:
In your routes.php, you'd define a route like:
Route::get("/users", "UsersController#show");
then in your Http folder define that given controller with it's name which you referred in above call and anything proceeding # symbol is a callback function which gets called automatically.
So in your http/UsersController.php, you'd have:
public function show(Request $request) {
//Do something with your request.
return "Something"; //could be an array or string or
//whatever since laravel automatically casts it into JSON,
//but it's strongly recommended to use transformers and compact method.
}
For more information try looking at laravel docs, they provide an amazing way to get started tutorial. Laravel Docs
I am working in Laravel 5.2 and i want to access URL segments in my controller. I am using
echo Request::segment(2);
but nothing is print. How can i get values from url in controller.
In laravel 5.2 you can do it this way..
echo request()->segment(2);
request() is one of the several helper functions provided in Laravel 5.2. It returns the current request object thus you don't need use statement for the facade on the top of your class.
In Laravel 7, I am using this to get segments
public function my_function(Request $request )
{
// By using this, we can get the second segment in route
// Example: example.com/hh/kk
$segment = $request->segment(2);
// By using this we will get "kk"
}
I'm using Slim framework. I've made an API with Post routes and Get routes
The Get ones are working perfectly
The Post ones are not.
this one is working when accessed via javascript or php
$app->get('/test',function(){
});
While this one return an error 404 not found when accessed
$app->post('/testpost',function(){
});
I can't figure out the problem
thank you for your help
Read the docs.
POST Route
You can add a route that handles only POST HTTP requests with the Slim application’s post() method. It accepts two arguments:
The route pattern (with optional named placeholders)
The route callback
$app = new \Slim\App();
$app->post('/books', function ($request, $response, $args) {
// Create new book
});
If you are posting your data and don't see it, that's because you're not passing any $request parameter to the callback.
The Slim's router is based on nikic/FastRoute, so if you prefer you may also refer to its docs to better understand it.
How are you testing?
Start up the PHP built in web server via php -S
and then I recommend using Curl:
curl -v -X POST http://localhost:8080/testform
I'm trying to add a new controller to an existing laravel project. The application already has some pages at /users and I am trying to add a RESTful API which works separately to this. I would like the API to be available at api/users.
I have created the controller using PHP artisan:
php artisan controller:make ApiUsersController
I have added the following to my routes:
Route::controller('api/users', 'ApiUsersController');
However when I hit the URL I just receive the site's 'Page could not be found' message.
Is there something I have missed?
It looks like the issue you're having is that you've used Route::controller rather than Route::resource.
Route::resource maps routes to the seven RESTful methods that the controller generator creates by default. Route::controller maps them to methods that you add yourself that have the HTTP method as part of their name, in your case if you had a method called getIndex it would be called on a GET request to /api/users/index or if you had one called postStore it would be called on a POST request to /api/users/store.
In order to add the API prefix to the route you could use the following:
Route::group(['prefix' => 'api'], function() {
Route::resource('users', 'ControllerName');
});
You could also add any other controllers in the API within the same callback.