Closed ericksuryadinata closed 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:
application/controllers/Api/v1/LectureApi.php
file exists?'api.lecture.index'
route does not work even under a jQuery AJAX request?super-admin/api/v1/lecturer
the AJAX request url?GET
the AJAX HTTP request method?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.
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.
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
If I am not mistaken, the problem is calling {{}} from jquery which it doesn't work as fas as I understood from the documentation.
$.ajax({
type: "GET",
url: "{{route('api.lecture.index')}}",
dataType: "JSON",
success: function (response) {
}
});
});
jQuery can't under stand {{route('api.lecture.index')}}
.
What i'm trying is,
inside api.php
but when I create jquery ajax, and refresh my page it give me error routing,.... but when I
inside web.php, it's not giving any error
So, how i can use api.php to build api's ??