LoginSignup
1
1

More than 1 year has passed since last update.

Laravelのドキュメントルートがpublicの際のURLの指定方法の使い分け

Last updated at Posted at 2023-01-13

はじめに

Laravelはデフォルトでドキュメントルートがpublicである。
ドキュメントルートの変更をしていない場合、web.phpに定義したURLにアクセスする時は手前にpublicを付ける必要がある。

Route::get('hello','HelloController@index');

実際にブラウザやcurlでURLを叩く際
(例)http://127.0.1.7/public/hello

しかし、実際のコーディング時URLを指定する際publicが必要なケースも存在する。
間違えて指定すると当然アクセスできないので備忘録としてまとめる。

種類

①ブラウザに直接入力する際
→public必須
(例)http://127.0.1.7/public/hello

②フォームタグで指定する場合
→public必須

<form action="/public/hello/edit" method="post">

③web.phpにルーティングとして記載する場合
→public不要

Route::get('hello','HelloController@index');

④redirectメソッドで指定する場合
→public不要

redirect('/hello');

⑤aタグで指定する場合
→public必須

<a href="/public/hello?sort=name">name</a>

まとめ

コーディング時、formタグやaタグ等を使うときはpublicが必要と覚えておけばOK。
dockerで作った環境でドキュメントルートの変更方法もいい加減確認しないとな・・・

1
1
1

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
1
1