Laravel-Lumen get request with parameter doesn't work - laravel

I'm facing a strange issue with lumen, all post and get request are working fine but only the get requests with parameter is not with below error
NotFoundHttpException
in RoutesRequests.php line 229
at Application->Laravel\Lumen\Concerns\{closure}(object(Request))
in RoutesRequests.php line 416
Here is my Web.php
$router->get('/', function () use ($router) {
return $router->app->version();
});
$router->group(['prefix' => 'api'], function () use ($router) {
$router->post('login','UserController#login');
$router->post('signup','UserController#signup');
$router->patch('profile','UserController#update');
$router->post('verfiy','UserController#verfiy');
$router->post('order','OrderController#store');
$router->get('userorders/{$uid}','OrderController#userOrder');
$router->get('locations/{$province}','LocationController#list');
$router->get('offers/{$province}','OfferController#list');
});
And this is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Offer;
class OfferController extends Controller
{
public function list($province)
{
$offers = Offer::where('province',$province)
->orderBy('num_orders', 'desc')
->paginate(20);
return response()->json(['status_code'=>1000,'data'=>$offers , 'message'=>null],200);
}
}
If i remove the parameter from the route and the controller it works and I have another Lumen project on same device and its works all fine with all requests !!??
Im on mac and apache
Any help will be much appreciated

You should define a route like:
$router->get('offers/{province}','OfferController#list');
and not like:
$router->get('offers/{$province}','OfferController#list');
Note the {province} difference.

Related

Route group prefix not working in Laravel 8

I was trying to make a group routing with a prefix in Laravel 8. But when I tested it in http://localhost/mysite/admin/test/, it always throws error 404.
Here is the code in web.php:
Route::prefix('/admin', function() {
Route::get('/test', [Admin\LoginController::class, 'index']);
});
I created a controller in app/Http/Controller/Admin/ as the controller is inside Admin folder.
Here is the code in LoginController:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class LoginController extends Controller
{
public function __construct()
{
//
}
public function index()
{
echo "Please login";
}
}
Can anybody show me what I am doing wrong to get it working?
You have to group the routes as stated in the documentation like:
Route::prefix('admin')->group(function () {
Route::get('/test', function () {
// Matches The "/admin/users" URL
});
});
In your case it would be:
use App\Http\Controllers\Admin\LoginController;
Route::prefix('admin')->group(function () {
Route::get('/test', [LoginController::class, 'index']);
});
I think it should be 'admin' not '/admin'.
That slash makes it:
http://localhost/mysite//admin/test
=>
http://localhost/admin/test
You can check all your routes using: php artisan route:list
Try this
use App\Http\Controllers\Admin\LoginController;
Route::prefix('admin')->group(function () {
Route::get('test', ['LoginController::class, index'])->name('test'); });

Laravel 8 (version 8.35.1): Target class does not exist

I'm using laravel 8.35.1 version. I have a api-resource controller "ProductController". At my route file api.php. I define the route this:
api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/products', 'App\Http\Controllers\ProductController');
Route::group(['prefix' => 'products'], function () {
Route::apiResource('/{product}/review', 'App\Http\Controllers\ReviewController');
});
NOTE:
It's work fine but, when i remove the complete path of controller like just write Route::apiResource('/products', 'ProductController'); it show error
Target class [ProductController] does not exist.
Before first clearing the cache. I want to get rid of the complete path. and second want to place the controllers in Api folder, so how to define route for that also.
I have also tried ProductController::class but not work fine
Updated
when I use the route according the laravel 8 doc. https://laravel.com/docs/8.x/controllers#resource-controllers it is working fine. but when move the controller file to Api folder then declare the route name space like use App\Http\Controllers\Api\ProductController; show error again
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ReviewController;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/products', ProductController::class);
Route::group(['prefix' => 'products'], function () {
Route::apiResource('/{products}/reviews', ReviewController::class);
});
You dont have to use class on declare controller in routing.
You may change 'ProductController' instead ProductController::class
When i try this then it work fine for me.
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ReviewController;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/products', 'App\Http\Controllers\ProductController');
Route::group(['prefix' => 'products'], function () {
Route::apiResource('/{product}/reviews', [ReviewController::class, 'ReviewController']);
});

Laravel get input data from POST request in a rest api

i'm trying to get input data which i post them from rest api as an json format, but in laravel i can't get them on controller and that return empty array of request
my api route:
Route::group(['prefix' => 'v1', 'namespace' => 'Api\v1'], function () {
$this->post('login', 'ApiController#login');
});
and ApiController:
<?php
namespace App\Http\Controllers\Api\v1;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class ApiController extends Controller
{
public function login(Request $request)
{
dd($request->all());
}
}
output:
[]
ScreenShot
Problem resolved by adding this line:
Content-type: text/json
to RestClient header
use Input Facade class In my case it was work
use \Illuminate\Support\Facades\Input;
$request = Input::All();

Laravel redirect route with a parameter from controller

I'm trying to redirect a route from a controller function after a form submit process in Laravel 5.4 as it is said in link below
https://laravel.com/docs/5.4/redirects#redirecting-named-routes
Route;
Route::group(['middleware' => ['api'], 'prefix' => 'api'], function () {
Route::post('doSomething', 'Page#doSomething');
});
Route::post('/profile', function () {
//..
});
Controller;
public function doSomething(Request $request){
return redirect()->route('profile', ['id'=>1]);
}
When I try to redirect I get this error.
InvalidArgumentException in UrlGenerator.php line 304: Route [profile]
not defined.
I have searched several times about redirection but I got similar results.
Any suggestions ? Thank you.
route() works with named routes, so name your route
Route::post('/profile', function () {
//..
})->name('profile');

Retrieve json data through route in Laravel 5.3

I am using Laravel 5.3 for my API and my frontend is not included in my view of Laravel. My frontend is on 80 port whereas my API is on 8080 port so whenever I want to make a communication in between I will call Laravel API.
I don't know how to retrive the requested JSON data in Laravel using post route and I want to return the same data in response just to check whether it is working fine or not.
so here is my route and controller(please guide me if i went wrong in my code):
Route::group(['middleware' => ['api','cors'],'prefix' => 'api'], function () {
Route::get('inquiry', 'inquiryController#store');
});
and controller is:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class inquiryController extends Controller
{
public function store(Request $request)
{
$data=$requst->json->all();
return response()->json([$data]);
}
}
But this code is not working properly. How can I solve this?
Try to do like this
public function store(Request $request)
{
$data = $request->all();
return response()->json($data);
}

Resources