前提
LARAVEL v9.33.0 plugin v0.6.1
を使用しています。
laravelでWEBアプリを開発しています。今までweb.phpを通して画面に表示していました。今まで会員制のページだけ作っていたので問題なくRote設定していたのですが、会員ではない(ログインしていない)人にも見られるようなページを作りたいです。
しかし例えば今まで
->middleware(['auth', 'verified'])
としていたものを書かなかったとしてもログインしていないと見られない状態になってしまいます。それはRouteServeProvider.phpで
Route::middlewere('web')
として'web'で定義しているからなのだと思います。'web'はkernel.phpで定義されていました。新しくmiddlewereしないRouteを定義しなければならないと思うのですが、そのために新しくフォルダーを作るべきか、或いはweb.phpなどの既存のフォルダーに書き込むことができるのかよく分かりません。
実現したいこと
ログインしていないユーザーでも見られるようなページを作りたいです。
今はログイン時のみURL('/techview')にアクセスできますが、ログインしていなくてもアクセスできるようにしたいです。
web.php
1<?php 2 3use Illuminate\Support\Facades\Route; 4use App\Http\Controllers\PostController; 5use App\Models\Post; 6use App\Http\Controllers\CommentController; 7use App\Http\Controllers\ProfileController; 8use App\Models\Role; 9use Symfony\Component\HttpKernel\Profiler\Profile; 10use App\Http\Controllers\RoleController; 11 12/* 13|-------------------------------------------------------------------------- 14| Web Routes 15|-------------------------------------------------------------------------- 16| 17| Here is where you can register web routes for your application. These 18| routes are loaded by the RouteServiceProvider within a group which 19| contains the "web" middleware group. Now create something great! 20| 21*/ 22 23Route::get('/', function () { 24 return view('welcome'); 25}); 26 27Route::get('/dashboard', function () { 28 return view('dashboard'); 29})->middleware(['auth', 'verified'])->name('dashboard'); 30require __DIR__.'/auth.php'; 31 32Route::get('/techview', function () { 33 return view('techview'); 34})->name('techview');
RouteServiceProvider.php
1<?php 2 3namespace App\Providers; 4 5use Illuminate\Cache\RateLimiting\Limit; 6use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; 7use Illuminate\Http\Request; 8use Illuminate\Support\Facades\RateLimiter; 9use Illuminate\Support\Facades\Route; 10 11class RouteServiceProvider extends ServiceProvider 12{ 13 /** 14 * Define your route model bindings, pattern filters, and other route configuration. 15 * 16 * @return void 17 */ 18 public function boot() 19 { 20 $this->configureRateLimiting(); 21 22 $this->routes(function () { 23 Route::middleware('api') 24 ->prefix('api') 25 ->group(base_path('routes/api.php')); 26 27 Route::middleware('web') 28 ->group(base_path('routes/web.php')); 29 }); 30 }
kernel.php
1/** 2 * The application's route middleware groups. 3 * 4 * @var array<string, array<int, class-string|string>> 5 */ 6 protected $middlewareGroups = [ 7 'web' => [ 8 \App\Http\Middleware\EncryptCookies::class, 9 \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, 10 \Illuminate\Session\Middleware\StartSession::class, 11 \Illuminate\View\Middleware\ShareErrorsFromSession::class, 12 \App\Http\Middleware\VerifyCsrfToken::class, 13 \Illuminate\Routing\Middleware\SubstituteBindings::class, 14 ], 15 16 'api' => [ 17 // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, 18 'throttle:api', 19 \Illuminate\Routing\Middleware\SubstituteBindings::class, 20 ], 21 ];
試したこと
・上述の通りweb.phpに表示したいページのRote設定をしたところ、
Attempt to read property "id" on null
のようにログインする必要があると言われた。ログイン時には見られる。
・他のコメントにある通り新たにroutesフォルダーにunlogin.phpを入れて、その中にRoute設定をしたが該当のRouteが見当たらないとされた。
・表示させたいファイル名をtechview.blade.phpから.bladeを抜いたところ、cssが一切表示されなくなった。
回答2件
あなたの回答
tips
プレビュー