在加载我的网页时,我一直遇到一个问题(路由未定义)。
这是我的路线(主要关注底部2条路线:
Route::group(['middleware' => ['web']], function()
{
Route::get('/', function ()
{
return view('welcome');
})->name('home');
Route::post('/signup', [
'uses' => 'UserController@postSignUp',
'as' => 'signup'
]);
Route::post('/signin', [
'uses' => 'UserController@postSignIn',
'as' => 'signin'
]);
Route::get('logout', [
'uses' => 'UserController@getLogout',
'as' => 'logout'
]);
Route::get('profile', [
'uses' => 'UserController@profile',
'as' => 'profile'
]);
Route::post('profile', [
'uses' => 'UserController@updateAvatar',
'as' => 'profile'
]);
Route::group(['middleware' => ['auth']], function()
{
Route::get('/dashboard',[
'uses' => 'UserController@getDashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);
});
Route::get('charities', [
'uses' => 'UserController@charities',
'as' => 'charities'
]);
Route::resource('charities','charityController');
});
因为它们的名字相同,我想也许这会导致错误,但我不太确定。
用户控制器:
<?php
namespace App\HTTP\Controllers;
use App\User;
use Illuminate\HTTP\Request;
use Illuminate\Support\Facades\Auth;
use Image;
use Input;
class UserController extends Controller
{
public function getDashboard()
{
return view('dashboard');
}
public function postSignUp(Request $request)
{
$this->validate($request, [
'email' => 'required|email|unique:users',
'name' => 'required|max:100',
'password' => 'required|min:4'
]);
$email = $request['email'];
$name = $request['name'];
$password = bcrypt($request['password']); //bcrypt = hash password
$user = new user();
$user->email = $email;
$user->name = $name;
$user->password = $password;
$user->save();
Auth::login($user);
return redirect()-> route('dashboard');
}
public function postSignIn(Request $request)
{
$this->validate($request, [
'email' => 'required|',
'password' => 'required'
]);
if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']]))
{
return redirect()->route('dashboard');
}
return redirect()->back();
}
public function getLogOut()
{
Auth::logout();
return redirect()->route('home');
}
public function profile()
{
return view('profile', array('user' => Auth::user()));
}
public function updateAvatar(Request $request)
{
if($request->hasFile('avatar'))
{
$avatar = $request->file('avatar');
$filename = time(). '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatar/' . $filename) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('profile', array('user' => Auth::user()));
}
public function charities()
{
return view('charities');
}
}
&GT;
CharityController包含以下功能:
public function index()
{
$charities = charities::all();
return view('charities', ['charities' => $charities]);
}
这是慈善机构页面的链接:
<a href="{{ route('charities') }}"> Charities </a> </li>
这是我在迭代数据库中的慈善机构时使用的变量:
@foreach ($charities as $charity)
我看到的错误是:
路线[慈善机构]未定义。 (视图: C:\ wamp64 \ WWW \ webproject \资源\视图\ profile.blade.php)
当我从路线中注释掉这一行时:
路线::资源(&#39;慈善机构&#39;&#39; charityController&#39);
然后错误消失,配置文件页面正常工作,反之亦然。
答案 0 :(得分:0)
问题是:Route::resource('charities','charityController');
已包含get routes
并且您已在
Route::get('charities', [
'uses' => 'UserController@charities',
'as' => 'charities'
]);
因此,您可以删除此get
,或者如果有必要,您可以保留它,然后将您的命名提供给Route::resource('charities','charityController');
像:
Route::resource('charities', 'AdminUserController', ['parameters' => [
'charities' => 'admin_user'
]]);
然后它会被视为/charities/{admin_user}
更多:https://laravel.com/docs/5.3/controllers#restful-naming-resource-routes
希望有意义
答案 1 :(得分:0)
你应该删除这些行
Route::get('charities', [
'uses' => 'UserController@charities',
'as' => 'charities'
]);
并在视图中使用此路线
<a href="{{ route('charities.index') }}"> Charities </a> </li>
因为您似乎正在制作冗余路由和控制器功能。由于慈善机构是资源控制器,因此这些是可用的路线
charities.index
charities.create
charities.store
charities.edit
charities.update
charities.destroy