I'm attempting to edit someone's premade script since the original owner hasn't updated it for a couple years and I'm unable to get in contact them. I think the script is written in Laravel but I don't know what version they used. For now it doesn't seem to work in the latest laravel version 8
Now my problem is when I try to login it will give error:
Illuminate \ Routing \ Exceptions \ UrlGenerationException Missing required parameters for [Route: dashboard.profiles.show] [URI: dashboard/{profile}].
Does anyone know how to fix this? here are some files that I think is related to the error
web.php route
use Illuminate\Support\Facades\Route;
Auth::routes(['verify' => true]);
Route::get('social/{provider}', 'Auth\SocialiteController@redirectToProvider')->name('auth.socialite');
Route::get('social/{provider}/callback', 'Auth\SocialiteController@handleProviderCallback');
Route::get('/', 'HomeController@index')->name('home');
Route::get('about', 'PageController@about')->name('pages.about');
Route::get('privacy', 'PageController@privacy')->name('pages.privacy');
Route::get('terms', 'PageController@terms')->name('pages.terms');
Route::get('press', 'PageController@press')->name('pages.press');
Route::get('l/{uid}', 'LinkController@show')->name('links.show');
Route::middleware('auth')->group(function () {
Route::view('subscribe', 'subscribe');
Route::get('account', 'UserController@edit')->name('users.edit');
Route::patch('account', 'UserController@update');
Route::patch('account/password', 'UserController@changePassword');
Route::post('referral/invites', 'ReferralController@invites')->name('referral.invites');
});
Route::get('sitemap.xml', 'SitemapController@sitemap')->name('sitemap');
Route::get('{profile}', 'ProfileController@show')->name('profiles.show');
dashboard.php route
use Illuminate\Support\Facades\Route;
Route::group(['middleware' => 'verified'], function () {
Route::get('profile/create', 'ProfileController@create')->name('profiles.create');
});
// api
Route::group(['prefix' => 'api/{profile}'], function () {
Route::get('/edit', 'ProfileController@edit');
Route::put('/update', 'ProfileController@update');
Route::post('/create', 'ProfileController@store');
Route::put('theme/update', 'ProfileController@update');
Route::put('/links/create', 'LinkController@store');
Route::delete('/links/{link}', 'LinkController@destroy');
Route::put('/links/{link}', 'LinkController@update');
Route::put('/links/{link}/resort', 'LinkController@resort');
});
Route::get('{profile}', 'ProfileController@show')->name('profiles.show');
ProfileController.php
<?php
namespace App\Http\Controllers;
use App\Models\Profile;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
/**
* Display the specified resource.
*
* @param Profile $profile
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(Profile $profile)
{
$profile->viewed();
return view('profiles.show', compact('profile'));
}
}
CodePudding user response:
There is no route dashboard.profiles.show
available, but only profiles.show
...
which might be a typo in the Blade template, which generates the link by named route.
The error message means: accessing a named route, which had not been registered.
UrlGenerationException
should not be thrown when referring to a known route.
It might generally be easier to pass int $id
instead of Profile $profile
. So how are you going to convert that object
into string
URL ?? {profile}
does not appear to be a viable approach.
I'd write it whole different (notice the singular form):
Route::get('/dashboard/profile', [
'as' => 'profile.show',
'uses' => 'ProfileController@show'
]);
Route::get('/profiles/{id}', [
'as' => 'profile.show',
'uses' => 'ProfileController@show'
]);
With a method signature eg. alike this (it could also be two methods):
/**
* @param int $id
* @return View
*/
public function show(int $id): View {
if (isset($id)) {
/* process parameter ID */
} else {
/* the own ID: Auth::user()->id */
}
}
And then look up the user ID in ProfileController
(if required). Passing an {id}
parameter only makes sense when viewing other profiles than the current user's profile - there's no right or wrong, but the controller should handle this; route parameters are not necessarily required. int $id
is convenient, because Eloquent also uses int $id
; therefore it's almost as good as the model instance Profile $profile
itself. Consistent grammar for the route names makes life easier ...because 'profiles.show'
rather sounds alike a listing of N
items, than a detail view of 1
.
The registered routes can also be listed with:
php artisan route:list