Bagisto Forum

    Bagisto

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    How to create the new api in bagisto?

    General Discussion
    5
    11
    2573
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      Ravinder G last edited by

      1. As per the requirement, we need to create new custom APIs for a mobile app.
      2. We need to change the success and failure messages in the existing APIs.
      3. We need to add the new fields in the registration API?

      Can you please let me know the details?
      How to customize the apis in bagisto structure?

      1 Reply Last reply Reply Quote 0
      • devansh-webkul
        devansh-webkul last edited by

        Hi @Ravinder-G,

        1. JWT is already setuped. You just need to add your route in the api.php.

        2. 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.

        3. You need to override the route.

        1 Reply Last reply Reply Quote 0
        • R
          Ravinder G last edited by

          @devansh-webkul Thanks for your reply.
          If possible can you please provide an example of how to override the existing apis in bagisto structure?

          1 Reply Last reply Reply Quote 0
          • devansh-webkul
            devansh-webkul last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • H
              hedyd last edited by

              @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.php

              Route::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

              http://meridiana.local/api/page/about

              Nam in quod velit ni

              1 Reply Last reply Reply Quote 0
              • ghermans
                ghermans last edited by

                Did you run the following command after changing the routes ?

                php artisan optimize
                

                Kind regards,
                Glenn Hermans

                Manager Bagisto Europe
                info@bagisto.eu

                H 1 Reply Last reply Reply Quote 0
                • H
                  hedyd @ghermans last edited by hedyd

                  @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.

                  Nam in quod velit ni

                  A 1 Reply Last reply Reply Quote 1
                  • A
                    alaa.sawaf @hedyd last edited by

                    @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

                    1 Reply Last reply Reply Quote 0
                    • devansh-webkul
                      devansh-webkul last edited by

                      Hi @hedyd

                      But why are you adding the middleware group again in the api.php file. api middleware group is already present in the api.php file.

                      Now the second thing there is no admin API we have released yet.

                      1 Reply Last reply Reply Quote 0
                      • H
                        hedyd last edited by

                        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

                        Nam in quod velit ni

                        1 Reply Last reply Reply Quote 0
                        • devansh-webkul
                          devansh-webkul last edited by

                          Not needed. Just simply place your route in the api.php file without any api middleware group also.

                          It will work else check Laravel docs to override because there is no Bagisto stuff.

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post