How to create the new api in bagisto?
-
Hi @Ravinder-G,
-
JWT is already setuped. You just need to add your route in the
api.php
. -
For success and failure message, if you are familiar with Laravel packages and upgrade. Then you can do it from the code otherwise you can override the route in the laravel app.
-
You need to override the route.
-
-
@devansh-webkul Thanks for your reply.
If possible can you please provide an example of how to override the existing apis in bagisto structure? -
Hi @Ravinder-G,
In
routes/api.php
you need to pass your package API routes which you need to override rest you can move it to your controller.As this is fully laravel stuff, you can check the laravel docs also.
-
@devansh-webkul said in How to create the new api in bagisto?:
routes
I put the code inside routes->api.php
But it still not working on my case. It saying not found
This is my code inside routes/api.phpRoute::get('page/{slug}', [PageController::class, 'index']);
this is my code inside controller. Mind you here that the controller i have created is inside Laravel app/http/controller folder
<?php namespace App\Http\Controllers\API; // use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Webkul\CMS\Http\Controllers\Controller; use Webkul\CMS\Repositories\CmsRepository; class PageController extends Controller { public function index($slug) { return "in"; } }
But when I'm hitting this api url but returing Not Found
-
Did you run the following command after changing the routes ?
php artisan optimize
-
@ghermans Yes, I did. It is still returning not found in postman. Let me share the code with you
routes > api.php
<?php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use Webkul\Admin\Http\Controllers\API\PageController; /* |-------------------------------------------------------------------------- | API Routes |-------------------------------------------------------------------------- | | Here is where you can register API routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | is assigned the "api" middleware group. Enjoy building your API! | */ Route::group(['middleware' => ['api'], 'prefix' => 'api'], function() { Route::get('page/{slug}', [PageController::class, 'presenter'])->name('shop.cms.page'); });
packages/Webkul/Admin/src/Http/Controllers/API/PageController.php
<?php namespace Webkul\Admin\Http\Controllers\API; use Webkul\CMS\Http\Controllers\Controller; use Webkul\CMS\Repositories\CmsRepository; class PageController extends Controller { /** * CmsRepository object * * @var \Webkul\CMS\Repositories\CmsRepository */ protected $cmsRepository; /** * Create a new controller instance. * * @param \Webkul\CMS\Repositories\CmsRepository $cmsRepository * @return void */ public function __construct(CmsRepository $cmsRepository) { $this->cmsRepository = $cmsRepository; } /** * To extract the page content and load it in the respective view file * * @param string $urlKey * @return \Illuminate\View\View */ public function presenter($urlKey) { $page = $this->cmsRepository->findByUrlKeyOrFail($urlKey); return json_encode(['page' => $page]); } }
If I put the above, route code, under Admin/Routes/cms-routes.php folder. Then it worked fine.
But I need to put my all API code in one file and wanna make jwt authentication on the API, which didn't work in my case. It doesn't matter if I do pass the Barear token or not, or pass the token param on the header or not, It still response me the data.
I want to keep one thing in notice that, in last comment, I was trying to create a Controller inside default Laravel Controller folder and referred that file from the route file.
But in the latest comment, I'm referring Package controller file rather then default Controller.
-
@hedyd
i am also wanting to add new route API to get custom products query
the documentation is very poor about thus stuff
please provide a detailed steps and sample code -
Hi @hedyd
But why are you adding the middleware group again in the
api.php
file.api
middleware group is already present in theapi.php
file.Now the second thing there is no admin API we have released yet.
-
Yes that is the case, But i don't need any admin API. All want is to create API which get data from DB. But in my case I have to create all my API under Admin folder
-
Not needed. Just simply place your route in the
api.php
file without anyapi
middleware group also.It will work else check Laravel docs to override because there is no Bagisto stuff.