Voyager Laravel axios get 405 - laravel

What could be wrong that I get a 405 method not allowed if Im gonna use a POST but if I used GET it gives me 200.
This is my Controller
public function getAnken(Request $request)
{
$from = $request->input('from');
$to = $request->input('to');
echo json_encode($request);
}
This is my Axios in Vuejs
axios.post('/admin/ankens',{
from: '2018/05/28',
to: '2018/05/29'
})
.then(function(response){
console.log(response.data)
});
This is my web.php
Route::group(['prefix' => 'admin'], function () {
Voyager::routes();
Route::get('/ankens', 'AnkensController#getAnken');
});
And I put this in my bootstrap.js too
window.axios = require('axios');
axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content');

Voyager::routes(); overwrites the below route Route::get('/ankens', to make it read first put it before.
Route::group(['prefix' => 'admin'], function () {
Route::post('/ankens', 'AnkensController#getAnken');
Voyager::routes();
});
Be sure to use URL/admin/slug-name and URL/admin/ankens are identical but different http verb.

Related

Get route is not working in laravel when i hit get route using api its return nothing

when I hit get route it returns this.I tried to get routes by using this API laravel
Route::group(['prefix' => 'admin', 'middleware' => ['auth:admins']], function() {
// Route::group(['prefix' => 'admin'], function() {
Route::post('details', [PassportAuthController::class, 'details']);
// get all list of
Route::apiResource('/admins', AdminController::class);
// opening hours
Route::apiResource('openinghours', OpeningHourController::class);
// orders
Route::apiResource('admin-orders', AdminOrderController::class);
// Categories
Route::apiResource('admin-categories', AdminCategoryController::class);
Route::post('add-push-token', [PushTokenAdminController::class, 'store']);
// sizegroups
Route::apiResource('size-groups', AdminSizeGroupController::class);
// sizegroups
Route::apiResource('size-groups.size', SizeController::class);
// products
Route::apiResource('admin-products', AdminProductController::class);
// accessories
Route::apiResource('admin-accessory', AdminAccessoryController::class);
Route::post('product-accessory-update', [AdminAccessoryController::class, 'productAccessoryUpdate']);
// products accessories
Route::apiResource('admin-product-accessory', AdminProductAccessoryController::class);
// Route::get('me', [AdminController::class, 'admin']);
Route::get('me', [PassportAuthController::class, 'details']);
Route::post('category-image-upload', [AdminCategoryController::class, 'uploadImage'])->name('category.image.upload.post');
Route::post('category-image-delete', [AdminCategoryController::class, 'deleteImage'])->name('category.image.delete');
// Route::post('product-image-upload', [AdminProductController::class, 'uploadImage'])->name('product.image.upload.post');
Route::post('product-image-delete', [AdminProductController::class, 'deleteImage'])->name('product.image.delete');
// sending push notifciatin sued for testing
Route::post('sendPushNotification', [PushNotificationController::class, 'send']);
});
You define post mathod and postman call get method.
use match and any method you can access post and get method
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});

laravel8 Undefined variable: errors

I am working on a Laravel Api with Vue js Front end. I have a problem on verify email and reset password. I am sending email using sendGrid In email of reset password button its redirects me to /api/password/reset and this route showing me exception of Undefined variable: errors (View: Path to/ reset.blade.php same is the case with verify email its redirect route is api/email/verify/ which is showing error exception in Undefined variable: errors (View: Path to/login.blade.php.
api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('users/send-email', 'App\Http\Controllers\Api\UsersController#sendEMails');
Route::post('client/search', 'App\Http\Controllers\Api\ClientsController#searchBy');
Auth::routes(['verify' => true]);
Route::apiResource('user', 'App\Http\Controllers\Api\UsersController');
Route::apiResource('freelancer', 'App\Http\Controllers\Api\FreelancersController');
Route::apiResource('client', 'App\Http\Controllers\Api\ClientsController');
Route::apiResource('service', 'App\Http\Controllers\Api\ServicesController');
Route::post('service/{id}', 'App\Http\Controllers\Api\ServicesController#update');
Route::post('client/{id}', 'App\Http\Controllers\Api\ClientsController#update');
Route::post('freelancer/{id}', 'App\Http\Controllers\Api\FreelancersController#update');
Route::apiResource('contact_us', 'App\Http\Controllers\Api\ContactUsController');
Route::post('clients/client-referal', 'App\Http\Controllers\Api\ClientsController#findReferal');
Route::post('freelancers/search', 'App\Http\Controllers\Api\FreelancersController#searchBy');
Route::post('services/search', 'App\Http\Controllers\Api\ServicesController#searchBy');
// getting admin users for admin dashboard
Route::get('users/admins', 'App\Http\Controllers\Api\UsersController#getAdminUsers');
Route::apiResource('sales_methods', 'App\Http\Controllers\Api\SalesMethodsController');
Route::apiResource('industries', 'App\Http\Controllers\Api\IndustriesController');
Route::middleware('auth:sanctum')->group(function () {
Route::apiResource('user_subscribe_client', 'App\Http\Controllers\Api\SubscribedUserClientsController');
Route::apiResource('apply_job', 'App\Http\Controllers\Api\UserAppliedJobController');
Route::get('users/setup-intent', 'App\Http\Controllers\Api\UsersController#getSetupIntent');
Route::post('users/payments', 'App\Http\Controllers\Api\UsersController#postPaymentMethods');
Route::post('clients/payments', 'App\Http\Controllers\Api\ClientsController#showMyPayments');
Route::get('users/payment-methods', 'App\Http\Controllers\Api\UsersController#getPaymentMethods');
Route::post('users/remove-payment', 'App\Http\Controllers\Api\UsersController#removePaymentMethod');
Route::put('users/subscription', 'App\Http\Controllers\Api\UsersController#updateSubscription');
Route::put('users/update-password/{id?}', 'App\Http\Controllers\Api\UsersController#updatePassword');
Route::post('services/status', 'App\Http\Controllers\Api\ServicesController#changeStatus');
});
**Web.php**
<pre>
Route::middleware('auth')->get('/user', function (Request $request) {
return $request->user();
});
Auth::routes(['verify' => true]);
Route::get('/{any?}', function () {
return view('welcome');
})->where('any', '^(?!api\/)[\/\w\.\,-]*');
Vuejs Password reset form action
async resetPassword() {
const post = { email: this.resetEmail };
const response = await axios
.post("/index.php/api/password/email", post).then((){
further logic
}).catch((){
further logic
})

Laravel route group prefix - variable not working

in web.php :
Route::group(['middleware'=>['checklang','checkmoney']],function(){
Route::get('/', function () {
return redirect('/'.session()->get('lang'));
});
Route::group([
'prefix' => '{locale}',
'where'=>['locale'=>'[a-zA-Z]{2}']],
function() {
Route::get('/tour/{id}','HomeController#getTours');
});
});
in HomeContoller :
public function getTours($id){
dd($id);
}
when trying to access url : example.com/en/tour/5
getting result
en , but should be 5
Where is a problem and how to solve it?
Your route has 2 variables, {locale} and {id}, but your Controller method is only referencing one of them. You need to use both:
web.php:
Route::group(['prefix' => '{locale}'], function () {
...
Route::get('/tour/{id}', 'HomeController#getTours');
});
HomeController.php
public function getTours($locale, $id) {
dd($locale, $id); // 'en', 5
}
Note: The order of definition matters; {locale} (en) comes before {id} 5, so make sure you define them in the correct order.

Laravel 5.2 to 5.3 - converting routes.php to web and api route files?

I am upgrading my guitar lessons site from 5.2 to 5.3. My previous developer has a full time job so now I am the site owner and developer... I am a better guitarist than developer...
My site has a backend for adding guitar lesson content. The routes.php file, among other things, had these lines:
function RegisterResourceRoute($route, $name) {
Route::get($route, 'Api\\'.$name.'Controller#getAll');
Route::get($route.'/{id}', 'Api\\'.$name.'Controller#get');
Route::post($route, 'Api\\'.$name.'Controller#post');
Route::put($route.'/{id}', 'Api\\'.$name.'Controller#put');
Route::delete($route.'/{id}', 'Api\\'.$name.'Controller#delete');
}
// Api
Route::group(['middleware' => ['api', 'auth', 'admin']], function () {
Route::group(array('prefix' => 'api'), function() {
RegisterResourceRoute('file', 'File');
RegisterResourceRoute('photo', 'Photo');
RegisterResourceRoute('category', 'Category');
RegisterResourceRoute('tag', 'Tag');
RegisterResourceRoute('lesson', 'Lesson');
RegisterResourceRoute('exercise', 'Exercise');
RegisterResourceRoute('user', 'User');
});
Route::post('/api/email', 'Api\\EmailController#send');
});
Then I see in the angular related js used in the view, this for the Exercise resource:
// Exercise
.when('/exercise', {
templateUrl: 'views/dashboard/exercise/view-all.html',
controller: 'ExerciseViewAllController'
})
.when('/exercise/create/:lessonId?', {
templateUrl: 'views/dashboard/exercise/create.html',
controller: 'ExerciseCreateController'
})
.when('/exercise/view/:id', {
templateUrl: 'views/dashboard/exercise/view.html',
controller: 'ExerciseViewController'
})
.when('/exercise/edit/:id', {
templateUrl: 'views/dashboard/exercise/edit.html',
controller: 'ExerciseEditController'
})
.when('/exercise/edit/:id/details', {
templateUrl: 'views/dashboard/exercise/edit-details.html',
controller: 'ExerciseDetailsController'
})
.when('/exercise/edit/:id/photos', {
templateUrl: 'views/dashboard/exercise/edit-photos.html',
controller: 'ExercisePhotosController'
})
.when('/exercise/delete/:id', {
templateUrl: 'views/dashboard/exercise/delete.html',
controller: 'ExerciseDeleteController'
})
The ViewAllController, for instance, looks like this:
angular.module('dashboard')
.controller('ExerciseViewAllController', ['$scope', '$http', function ($scope, $http) {
$scope.exercises = [];
$scope.refresh = function () {
$scope.exercises = [];
load();
};
var load = function () {
$scope.busy = true;
$http.get('/api/exercise').then(function (response) {
$scope.busy = false;
$scope.exercises = response.data;
}, function (response) {
$scope.busy = false;
});
};
load();
}]);
I am trying to figure out how 5.3 handles such routing. In my backend dashboard, when I try and view the list of exercises, for instance, I was getting a 404 error for api/exercise.
So I created web.php with this under admin routes:
Route::resource('api/exercise', 'Api\ExerciseController');
And in api.php
Route::group(['prefix' => 'api'], function () { Route::resource('exercise', 'Api\ExerciseController'); });
It now works, but I had to change what was previously a getAll() function in the Exercise controller to index(), otherwise there was a error since laravel was looking for the default index method.
I would rather get it working using default route names, and also I have no idea if what I added to web.php and api.php is really the correct way. I see things seem to work but I am worried I am not seeing the whole picture...
Can someone explain what I would do to get these routes working in the true laravel 5.3 way?
thanks!
Not remember exactly but I upgraded from 5.2 to 5.3 by this link https://laravel.com/docs/5.3/upgrade
Do not get overwhelmed by the content. just search for bits that you are getting errors from. for example routes section.
Hope that helps

how to use the laravel subdomain routing function

I am using the following code its from the laravel 4 site
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('user/{id}', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
the idea is to redirect subdomain.myapp.com to myapp.com/user/subdomain
.what I have is not working any suggestions?sorry I just started with laravel about a month now.
Remove user/{id} and replace it with / and then use the following url https://accountname.myapp.com and it will redirect to https://www.myapp.com/accountname
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
Edit the answer to the correct answer
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account) {
// ...
return Redirect::to('https://www.myapp.com/'.$account);
});
});
as Marc vd M answer but remove $id from get's closure function.
why use 'https://www.myapp.com'.'/'.$account and not 'https://www.myapp.com/'.$account

Resources