Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jet's avatar
Level 1

How can i create am Api from this. With out it routing back to the Login page

please anyone help I'm trying to create an API for properties

api.php

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

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

    

        Route::group(['prefix' =>'properties'],function () {
        Route::get('', 'PropertyController@index');
        Route::get('{id}', 'PropertyController@show');
        Route::post('', 'PropertyController@store');
        Route::put('{id}', 'PropertyController@update');
        Route::delete('{id}', 'PropertyController@destroy');

    });


});

wap.php


Route::get('/login', 'AuthController@loginForm')->name('login');
Route::get('/register', 'AuthController@registerForm')->name('register');
Route::get('/reset-password/email', 'AuthController@confirmEmailForm');
Route::get('/reset-password/password', 'AuthController@resetPasswordForm');
Route::post('/auth/register', 'AuthController@register');
Route::post('/auth/login', 'AuthController@login');
Route::post('/auth/logout', 'AuthController@logout');

Route::get('/account', 'HomeController@index')->name('account')->middleware('role:admin'); 
Route::get('/profile', 'HomeController@profile');

Route::resource('posts', 'PostsController');
Route::resource('categories', 'CategoryController');
Route::resource('properties', 'PropertyController');
Route::resource('property-categories', 'PropertyCategoryController');


Route::get('/account', 'ProfileController@account');
Route::get('/personal-information', 'ProfileController@personalInformation');

Route::get('/users', 'ProfileController@getUsers');
Route::get('/user/{id}', 'ProfileController@getUser');

Route::post('reset/password', 'AuthController@resetPassword');
0 likes
5 replies
aurawindsurfing's avatar

Hey @jet

Not sure if you know but in Laravel if you do not return a view in your controller but instead you just return what you send to the view you will actually get proper json response, try this:

Route::get('/', function () {

    $fooBar = [
        'foo' => 'bar'
    ];

    return $fooBar;
});

this will return:

{
foo: "bar"
}

Now, this is absolutely not safe and not recommended but it might give you an idea. You actually already have everything you need you just need to encapsulate it in api routes with some sort of authentication in place.

Hope it helps!

jet's avatar
Level 1

@AURAWINDSURFING - thanks for the help but I'm still getting redirected to the login page after doing this api.php

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

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

        Route::get('property', 'PropertyController@propertyjson');
});


Route::post('reset/password', 'AuthController@resetPassword');

web.php

Route::resource('/properties', 'PropertyController')->except('propertyjson');

propertyController


    public function propertyjson()
    {
      
            $properties = Property::all();
             return  ['properties' => $properties];
            // return PropertyResource::collection($properties);
    }

        public function index()
    {
            $properties = Property::orderBy('id', 'asc')->paginate(20);

             return view('properties.index', ['properties' => $properties]);
    }

    ```
shez1983's avatar

whats 'secure.api' middleware? what does ur routeserviceprovider look like? how are you accessing these routes?

aurawindsurfing's avatar

You need to build up gradually your experience with laravel. It looks like you are trying to implement the whole thing without understanding what each of the puzzles does.

Try breaking it into smaller steps, prove it works without the secure.api, then show us what secure.api actually is and what it does.

You need to take a baby steps and not big leaps ;-)

Also to test api you might use something like Postman.

Nee's avatar

This is just a hunch but if you are being redirected to the login page all the time, the routes you are calling might be protected. Look in the app/exceptions folder. In your Handler Class, add this.


   protected function unauthenticated($request, AuthenticationException $exception)
    {

         return $request->expectsJson()
                ? response()->json([
                        'code'=> '401',
                        'message' => "Not authenticated Please login"
                        ]);

    }


Please or to participate in this conversation.