6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel5.3で/apiをURLから消す方法

Posted at

結論

  • RouteServiceProvider.phpのmapApiRoutesメソッドのprefixをnullにする。
 69     protected function mapApiRoutes()
 70     {
 71         Route::group([
 72             'middleware' => 'api',
 73             'namespace' => $this->namespace,
 74             'prefix' => null,                                                    // ココ!!
 75         ], function ($router) {
 76             require base_path('routes/api.php');
 77         });
 78     }
 79 }

frameworkにあらかじめある、routes/api.phpをつかいたい

  • api.php名前ついてるから、apiはこれつかうよね普通
  • ところが、URLは/apiが必ず固定される、http://localhost/api/hogehoge てな感じ

route.php

  • https://hogehgoe.com//hoge みたいになって泣きました。
  • 結局Laravelのソースコードを追いかけると
  • Illuminate/Routing/Router.php
 503     /**
 504      * Prefix the given URI with the last prefix.
 505      *
 506      * @param  string  $uri
 507      * @return string
 508      */
 509     protected function prefix($uri)
 510     {
 511         return trim(trim($this->getLastGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/';    // これだから
 512     }


 426     /**
 427      * Get the prefix from the last group on the stack.
 428      *
 429      * @return string
 430      */
 431     public function getLastGroupPrefix()
 432     {
 433         if (! empty($this->groupStack)) {
 434             $last = end($this->groupStack);
 435
 436             return isset($last['prefix']) ? $last['prefix'] : '';    // これで
 437         }
 438
 439         return '';
 440     }
  • prefixの行を消すのではなくて、nullにしてやれば思い通りのURL体系になります。
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?