ingeniasoftware / luthier-ci

Improved routing, middleware support, authentication tools and more for CodeIgniter 3 framework
https://luthier.ingenia.me/ci/en/
MIT License
150 stars 38 forks source link

How can I use api.php to make API ? #14

Closed ericksuryadinata closed 6 years ago

ericksuryadinata commented 6 years ago

What i'm trying is,

Route::group('super-admin', function(){
    Route::group('api',['namespace'=>'Api'],function(){

        Route::group('v1',['namespace' => 'v1'], function(){
            Route::get('lecturer','LectrureApi@index')->name('api.lecture.index');
        });
    });
});

inside api.php

but when I create jquery ajax, and refresh my page it give me error routing,.... but when I

require __DIR__ 'api.php'; 

inside web.php, it's not giving any error

So, how i can use api.php to build api's ??

andersonsalas commented 6 years ago

Hi!

All routes defined in api.php are available under AJAX requests only, this is detected by Luthier-CI at boot time and you don't need to include that file manually.

You can use any web.php route with AJAX requests, as you may noticed, but not vice versa. Before giving this behavior as a possible bug we will first determine some things:

  1. Does the application/controllers/Api/v1/LectureApi.php file exists?
  2. The 'api.lecture.index' route does not work even under a jQuery AJAX request?
  3. Is super-admin/api/v1/lecturer the AJAX request url?
  4. Is GET the AJAX HTTP request method?
  5. Is the given error a 404 or a Route not found error?
ericksuryadinata commented 6 years ago

Thanks for your response

in my api.php

Route::group('super-admin', function(){
    Route::group('api',['namespace'=>'Api'],function(){

        Route::group('v1',['namespace' => 'v1'], function(){
            Route::get('lecture','LectureApi@index')->name('api.lecture.index');
        });
    });
});

and my jQuery AJAX

$("#edit").on('click', function () {
            $.ajax({
                type: "GET",
                url: "{{route('api.lecture.index')}}",
                dataType: "JSON",
                success: function (response) {

                }
            });
        });

and it give me an error, not in the request, but whole page. and when i use route from web.php it not give me an error.

I already try to echo the route from api.php, but it still give me error (whole page) I think api.php is not loaded, because when i move the route from api.php to web.php and echo them, it give me the link.

andersonsalas commented 6 years ago

Hmmm. I think that I have found the problem:

You have defined an AJAX route in api.php file, and then when you retrieve it inside any web.php route/controller Luthier-CI crashes (throws a RouteNotFoundException)

Apparently, the only scenario where Luthier-CI throws that exception is this:

#application/routes/web.php

Route::get('test', function(){
    echo route('api_test'); // <-- The 'api_test' route does not exists in this context
});
#application/routes/api.php

Route::get('api/test', function () {
    echo json_encode(['msg' => 'Hello World!']);
})->name('api_test');

A solution could be to make all api.php routes available in web.php, but maintaining the idea that api.php is intended for AJAX routes. So, in the next release Luthier-CI will inject automatically a middleware that reject all non-AJAX requests over any api.php defined route.

ericksuryadinata commented 6 years ago

thanks for the response, for right now, I'll leave the best practice and use web.php instead api.php, because i'm on deadline :D, but i'm looking for the future routing in luthier

Ihabafia commented 5 years ago

If I am not mistaken, the problem is calling {{}} from jquery which it doesn't work as fas as I understood from the documentation.

$("#edit").on('click', function () {

        $.ajax({
            type: "GET",
            url: "{{route('api.lecture.index')}}",
            dataType: "JSON",
            success: function (response) {   
            }
        });
    });

jQuery can't under stand {{route('api.lecture.index')}}.