Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel api authorization with api_token

I am trying to create a Laravel API project. So I have this project with basic laravel's scaffolding set up. In my user migration I have added:

$table->string('api_token', 60)->unique();

then in my User.php model i have added:

 protected $fillable = [
    'name', 'email', 'password','api_token'
];

Then in my api.php i have made a test route:

Route::group(['middleware' => ['auth:api']], function(){

Route::get('/test', 'ApiController@test');

});

in my Apicontroller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ApiController extends Controller
{


public function test(Request $request){

return response()->json(['name' => 'test']);


}



}

so now i type this : with my api_token

localhost/project1/public/api/test?api_token='hsvdvhvsjhvasdvas8871238'

It's not giving me the json data, instead it's redirecting to the logged in home page

like image 701
Manas Avatar asked Dec 18 '25 01:12

Manas


1 Answers

localhost/project1/public/index.php/api/test?api_token='hsvdvhvsjhvasdvas8871238' would help.

If you want pretty urls, read the documentation: Pretty URLs

like image 184
Cong Chen Avatar answered Dec 20 '25 16:12

Cong Chen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!