Lumen directing all routes of specific group to * - laravel

I'm developing a website with simple landing page and a few other page such as contacts and news as front end (which uses normal PHP and lumen should be sufficient bythe way) and vuejs as backend. Trying to send all get request from '/admin/' to view('admin'). This is the best I could come up with...
$router->group(['prefix' => 'admin'], function () use ($router) {
$router ->get('/{route:.*}/', function () use ($router) { return view('admin');
});
The problem is all url I do or access, all get request response I get was 404 - not found. Not even any log or anything in lumen log or even in error log apache server. Where did I do wrong?
//using lumen since I need API for vuejs operations.

Related

How can I test the Api in Laravel via Postman?

I have a route like this:
Route::group(
[
'prefix' => 'v1', 'as' => 'v1.',
],
static function () {
Route::get('test', 'Api\TestController#test')->name('test');
///
How can I refer to this route so that it would work? I am trying to call this route in Postman http://localhost/api/v1/test but I get a 400 error
You didn't provide enough information but check these just to be sure
Make sure these routes are listed among the api routes and not the web
If you're using artisan to serve your project, then ensure you're using the correct port in postman too eg http://127.0.0.1:8000/api/v1/test
(Pretty obvious) Make sure you're sending a get request from postman
Check the function too to know if there's any problem. You could do an early return of a simple json response in the beginning of the function to make sure the route accessed the function at all

Laravel api - 405 not allowed shared hosting

I'm new to Laravel and i have the next problem.
I have an API POST route that works properly in the localhost. I send POST requests from POSTMAN:
Route::group(['middleware' => 'api_import'], function () {
Route::get('/products', [ArticlesController::class, 'index']);
Route::post('/products', [ArticlesController::class, 'addOrUpdateArticles']);
Route::post('/categories', [CategoriesController::class, 'addOrUpdateCategories']);
});
However, it will not work on hosting. Hosting is shared, and I get the message:
When json contains a couple of products everything works properly. When json contains a complete table of 3500 products I get this error.
It is possible that there is a redirect on the page. You should confirm this through the network tab in your browser, because any POST request with a redirect is considered a GET request, so an error appears (405 Method Not Allowd)

Exclude conditional routes in Laravel

I'm building and application in Laravel and Vuejs where I'm having Laravel routes as below:
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->where('view', '!=', 'admin')->name('home');
I'm using Vue-router so I'm having routing in vuejs, and I'm using history mode. The problem is when I try to call /admin it generally calls HomeController#home method. even if I go deeper like /admin/dashboard it is calling the same home method. I want if admin prefix is being called then it should call HomeController#admin method.
its all okay for me please check this
Route::get('/admin/{view?}', function (){
dd('okay');
})->where('view', '(.*)')->name('admin');
Route::get('/{view?}', function(){
dd('okay1');
})->where('view', '(.*)')->name('home');
So try this
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->name('home');

Vue SPA url not working with history mode enabled

I am creating a SPA using Vue js and it kind of works but I have one problem. With history mode enabled on Vue I can enter urls and go to that page using the Vue Router but when I try to login I get the literal page html. I know I can do something like auth\{vue?} but I would prefer if i didn't have to do that. I want to be able to always keep the url with no prefixes unless it is to an API request. So for example:
I have the root view:
Route::get('/{vue?}', function () {
return view('layouts.app');
})->where('vue', '[\/\w\.-]*');
and then I have the api requests:
Route::group(['prefix' => 'api'], function () {
Route::post('/login', 'Auth\\LoginController#login');
Route::post('/register', 'Auth\\RegisterController#register');
Route::get('/logout', 'Auth\\LoginController#logout');
});
but when I hit /api/login I get the return view data from /{vue?}.
I hope that makes sense and if so any help would be amazing, Thanks.
it is clearly explained here: https://kjamesy.london/work/laravel-53-vuejs-20-make-vuerouters-history-mode-play-nicely-with-laravels-routes
Short story: declare history mode to false in VueRouter and then on the Laravel side, whenever the $request->ajax() is false, the same route will always capture the request. So, we can safely define a catch-all route to redirect such traffic to the index() method of our resource controller:

Laravel & PHPUnit: Getting 500 when unit testing "Passport" restricted route

I have a Laravel 5.3 app.
Inside my api.php file, there's a route for posting an answer within a poll.
Route::group(['middleware' => 'auth:api'], function () {
Route::post('/poll/answer', 'API\PollsController#createAnswer');
});
The route is part of a group, restricted by the auth:api middleware, using Laravel's Passport engine.
When calling this route from Postman or any other tool to test APIs, I get 401 which is ok because I'm not attaching the token.
But when unit testing this call using PHPUnit, it returns 500. I have no idea why.
$this->postJson('api/poll/answer');
I'm probably missing a configuration or setup instruction.
Any ideas?
The 500 error was occurring because I forgot to add an app key to the .env.testing file.
It got solved after adding that.

Resources