閲覧ありがとうございます。
Laravelの初学者です。
現在、Laravelの自作アプリケーションを作成しながら勉強をしています。
環境
- ハードウェア環境
項目 | 情報 |
---|---|
OS | macOS Monterey(12.1) |
ハードウェア | MacBook Air |
チップ | Apple M1 |
- ソフトウェア環境
項目 | 情報 |
---|---|
PHP バージョン | 8.0 |
Laravel バージョン | 8.75 |
MySQLバージョン | 5.7.34 |
- 開発環境
項目 | 情報 |
---|---|
言語 | html,css(bootstrap5),js,php(Laravel8) |
エディター | vscode |
ログイン機能 | Laravel/ui |
やりたいこと
Laravel Auth ログイン後のリダイレクト先を動的に変更したい。
参考:Laravel Auth ログイン後のリダイレクト先をデフォルトから変更する リメイク編
例) 下のようなことがやりたいのですが、エラーが出てできません。
php
1$date = Carbon::now()->format('Y-m-j') 2public const HOME = '/show' . $date;
よろしくお願いします。
本体
php
1<?php 2 3namespace App\Http\Controllers\Auth; 4 5use App\Http\Controllers\Controller; 6use App\Providers\RouteServiceProvider; 7use Illuminate\Foundation\Auth\AuthenticatesUsers; 8use App\Facades\Calendar; 9 10class LoginController extends Controller 11{ 12 13 14 use AuthenticatesUsers; 15 16 /** 17 * Where to redirect users after login. 18 * 19 * @var string 20 */ 21 protected $redirectTo = RouteServiceProvider::HOME; 22 23 /** 24 * Create a new controller instance. 25 * 26 * @return void 27 */ 28 public function __construct() 29 { 30 $this->middleware('guest')->except('logout'); 31 } 32 33 34}
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 * The path to the "home" route for your application. 15 * 16 * This is used by Laravel authentication to redirect users after login. 17 * 18 * @var string 19 */ 20 public const HOME = '/show'; 21 22 /** 23 * The controller namespace for the application. 24 * 25 * When present, controller route declarations will automatically be prefixed with this namespace. 26 * 27 * @var string|null 28 */ 29 // protected $namespace = 'App\\Http\\Controllers'; 30 31 /** 32 * Define your route model bindings, pattern filters, etc. 33 * 34 * @return void 35 */ 36 public function boot() 37 { 38 $this->configureRateLimiting(); 39 40 $this->routes(function () { 41 Route::prefix('api') 42 ->middleware('api') 43 ->namespace($this->namespace) 44 ->group(base_path('routes/api.php')); 45 46 Route::middleware('web') 47 ->namespace($this->namespace) 48 ->group(base_path('routes/web.php')); 49 50 Route::pattern('date', '^[0-9]{4}-[0-9]{2}-[0-9]{1,2}$'); 51 }); 52 } 53 54 /** 55 * Configure the rate limiters for the application. 56 * 57 * @return void 58 */ 59 protected function configureRateLimiting() 60 { 61 RateLimiter::for('api', function (Request $request) { 62 return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip()); 63 }); 64 } 65} 66
使用している ui のバージョンは
"laravel/ui": "^3.4" です。
エラーですが、
「Too few arguments to function App\Http\Controllers\HomeController::show(), 0 passed in /Applications/MAMP/htdocs/Prototype_diary/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 and exactly 1 expected」
です。
web.php
「Route::get('/show/{date?}', [HomeController::class, 'show'])->where('date', '^[0-9]{4}-[0-9]{2}-[0-9]{1,2}$')->name('show');」
コントローラー
「public function show($date){....}」
回答2件
あなたの回答
tips
プレビュー