Hướng dẫn what is difference between api php and web php in laravel? - sự khác biệt giữa api php và web php trong laravel là gì?

Sau khi tạo dự án Laravel mới (tôi đang sử dụng V9), bạn sẽ tìm thấy nhiều tệp trong thư mục tuyến đường.

laravel-project $ tree routes/
routes/
├── api.php
├── channels.php
├── console.php
└── web.php

Tệp channels.php là để phát sóng và console.php để đóng lệnh. Hai cái còn lại, api.phpweb.php, khá giống nhau và cả hai cho các tuyến web. Nhưng chúng khác nhau như thế nào? Đối với điều này, chúng ta cần đạt được RouteServiceProvider (xem bên dưới).

Trong phương thức boot() của RouteServiceProvider, chúng ta thấy rằng cả hai tuyến API và Web đều được đăng ký. Họ được đăng ký với một vài khác biệt mặc dù.

Như bạn có thể thấy bên dưới có ba sự khác biệt đáng chú ý:

  • Có giới hạn tỷ lệ được cấu hình cho các tuyến API.
  • Các tuyến API sử dụng nhóm phần mềm trung gian
    
    // ...
    
    class RouteServiceProvider extends ServiceProvider
    {
        // ..
    
        /**
         * Define your route model bindings, pattern filters, and other route configuration.
         *
         * @return void
         */
        public function boot()
        {
            $this->configureRateLimiting();
    
            $this->routes(function () {
                Route::middleware('api')
                    ->prefix('api')
                    ->group(base_path('routes/api.php'));
    
                Route::middleware('web')
                    ->group(base_path('routes/web.php'));
            });
        }
    
        /**
         * Configure the rate limiters for the application.
         *
         * @return void
         */
        protected function configureRateLimiting()
        {
            RateLimiter::for('api', function (Request $request) {
                return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
            });
        }
    }
    
    1 nơi các tuyến web sử dụng nhóm phần mềm trung gian
    
    // ...
    
    class RouteServiceProvider extends ServiceProvider
    {
        // ..
    
        /**
         * Define your route model bindings, pattern filters, and other route configuration.
         *
         * @return void
         */
        public function boot()
        {
            $this->configureRateLimiting();
    
            $this->routes(function () {
                Route::middleware('api')
                    ->prefix('api')
                    ->group(base_path('routes/api.php'));
    
                Route::middleware('web')
                    ->group(base_path('routes/web.php'));
            });
        }
    
        /**
         * Configure the rate limiters for the application.
         *
         * @return void
         */
        protected function configureRateLimiting()
        {
            RateLimiter::for('api', function (Request $request) {
                return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
            });
        }
    }
    
    2.
  • API có tiền tố
    
    // ...
    
    class RouteServiceProvider extends ServiceProvider
    {
        // ..
    
        /**
         * Define your route model bindings, pattern filters, and other route configuration.
         *
         * @return void
         */
        public function boot()
        {
            $this->configureRateLimiting();
    
            $this->routes(function () {
                Route::middleware('api')
                    ->prefix('api')
                    ->group(base_path('routes/api.php'));
    
                Route::middleware('web')
                    ->group(base_path('routes/web.php'));
            });
        }
    
        /**
         * Configure the rate limiters for the application.
         *
         * @return void
         */
        protected function configureRateLimiting()
        {
            RateLimiter::for('api', function (Request $request) {
                return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
            });
        }
    }
    
    1, do đó các tuyến đường từ api.php được tiền tố với
    
    // ...
    
    class RouteServiceProvider extends ServiceProvider
    {
        // ..
    
        /**
         * Define your route model bindings, pattern filters, and other route configuration.
         *
         * @return void
         */
        public function boot()
        {
            $this->configureRateLimiting();
    
            $this->routes(function () {
                Route::middleware('api')
                    ->prefix('api')
                    ->group(base_path('routes/api.php'));
    
                Route::middleware('web')
                    ->group(base_path('routes/web.php'));
            });
        }
    
        /**
         * Configure the rate limiters for the application.
         *
         * @return void
         */
        protected function configureRateLimiting()
        {
            RateLimiter::for('api', function (Request $request) {
                return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
            });
        }
    }
    
    5.

Các nhóm phần mềm trung gian khác nhau có thể được tìm thấy trong


// ...

class RouteServiceProvider extends ServiceProvider
{
    // ..

    /**
     * Define your route model bindings, pattern filters, and other route configuration.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->group(base_path('routes/web.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });
    }
}
6.


// ...

class RouteServiceProvider extends ServiceProvider
{
    // ..

    /**
     * Define your route model bindings, pattern filters, and other route configuration.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->group(base_path('routes/web.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });
    }
}
  • Laravel

Tất cả các tuyến đường được đặt trong API.PhP sẽ được tiền tố bởi/API, cũng được Bernadd đề cập, có những khác biệt khác: trong liên kết này (https://mattstauffer.co/blog/routing-changes-in-taravel-5 -3) Bạn có thể tìm thấy sự khác biệt giữa API và Web trong mã Laravel:

Trong Ứng dụng \ Nhà cung cấp \ RouteserviceProvider:

public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        //
    }

    protected function mapApiRoutes()
    {
        Route::group([
            'middleware' => ['api', 'auth:api'],
            'namespace' => $this->namespace,
            'prefix' => 'api',
        ], function ($router) {
            require base_path('routes/api.php');
        });
    }

    protected function mapWebRoutes()
    {
        Route::group([
            'namespace' => $this->namespace, 'middleware' => 'web',
        ], function ($router) {
            require base_path('routes/web.php');
        });
    }

Trong ứng dụng \ http \ kernel.php trong "được bảo vệ $ meterwaregroup" Bạn có thể thấy điều này:

 'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

  'api' => [
            'throttle:60,1',
            'bindings',
        ],

Và: trong Config \ auth.php: Trong nhận xét của tệp này, bạn có thể tìm ra rõ ràng sự khác biệt giữa mặc định "Auth" ('Guard' => 'Web') so với "Auth: API" in config\auth.php : In this file's Comments you can clearly find out the difference between default "auth"('guard' => 'web') vs "auth:api"

API PHP ở Laravel là gì?

tuyến/API. Tệp PHP được sử dụng khi bạn muốn tạo API RESTful. Điều đó có nghĩa là bạn đang phát triển mặt trước của dự án với một thứ khác (ví dụ: Angular) và bạn muốn phát triển mặt sau của bạn bằng Laravel. Trong trường hợp đó, tất cả các tuyến của bạn sẽ đi theo các tuyến/API. PHP.used when you want to make a Restful API . It means that you are developing the front end of your project with something else(for example: angular) and you want to develope your back end using Laravel. In that case your all routes will go in routes/api. php .

Web php laravel là gì?

Tệp tuyến đường mặc định Các tuyến/tệp web.php xác định các tuyến đường dành cho giao diện web của bạn.Các tuyến này được chỉ định nhóm phần mềm trung gian web, cung cấp các tính năng như bảo vệ phiên và bảo vệ CSRF.Các tuyến đường trong các tuyến/API.php không trạng thái và được chỉ định nhóm phần mềm trung gian API.The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group.

Tại sao chúng ta cần API trong PHP?

Giao diện lập trình ứng dụng hoặc API, xác định các lớp, phương thức, chức năng và biến mà ứng dụng của bạn sẽ cần gọi để thực hiện nhiệm vụ mong muốn của nó.Trong trường hợp các ứng dụng PHP cần giao tiếp với cơ sở dữ liệu, các API cần thiết thường được hiển thị thông qua các tiện ích mở rộng PHP.defines the classes, methods, functions and variables that your application will need to call in order to carry out its desired task. In the case of PHP applications that need to communicate with databases the necessary APIs are usually exposed via PHP extensions.

Laravel có API không?

Giới thiệu.Theo mặc định, Laravel vận chuyển với một giải pháp đơn giản để xác thực API thông qua mã thông báo ngẫu nhiên được gán cho mỗi người dùng ứng dụng của bạn.Trong cấu hình/auth của bạn.Tệp cấu hình PHP, một người bảo vệ API đã được xác định và sử dụng trình điều khiển mã thông báo.By default, Laravel ships with a simple solution to API authentication via a random token assigned to each user of your application. In your config/auth. php configuration file, an api guard is already defined and utilizes a token driver.