Home  >  Q&A  >  body text

What are the differences between Laravel 5.3 and routing written in api.php and web.php?

What are the differences in routing processing written in api.php and web.php in Laravel 5.3?

I want to know, if you use template rendering to create a web page, and ajax requests are also used, is it better to put the routing of this ajax request in api.php or web.php?

PHP中文网PHP中文网2713 days ago423

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 16:52:28

    According to your description, it should obviously be placed in web.php.

    Because you just have an ordinary web project, the routing in web.php uses the web middleware group.

    protected $middlewareGroups = [
            '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',
            ],
        ];

    If you put ajax routing in api.php without csrf protection, it is easy to cause program vulnerabilities, except for public resource requests.

    api.php is used with Laravel passport to provide API services.

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 16:52:28

    Essentially the same.

    Put it wherever you feel like it.

    I think it’s better to put it in web.php. Because from your description, it doesn’t look like an API.

    reply
    0
  • Cancelreply