在 laravel 问题中更改索引 blade
Changing the index blade in laravel issue
我的 laravel web.php
中有关注者
Route::get('/', function () {
return view('home');
})->middleware('auth');
Route::get('/home', 'HomeController@index');
如果我的用户没有登录,这会将我的用户重定向回登录页面,而登录的用户会重定向到主页。
现在在我的家庭控制器索引函数中,我有以下代码,
public function index()
{
$get_customers = User::where('user_roles','=','customer')->get();
$count_customers = $get_customers->count();
$get_apps = Website::all();
$count_apps = $get_apps->count();
return view('home',compact('count_customers','count_apps'));
}
每次登录后尝试访问我的主页时,我都会收到一条错误消息
$count_apps is undefined
但是,
当我在我的 web.php
中使用以下路由而不是以前的路由时,主页没有错误并且工作正常
Route::get('/', function () {
return view('auth.login');
})
但是即使这使我的登录 blade 成为索引页面,每次当我尝试以已经登录的用户身份访问索引时,它都会将我重定向到登录 blade 而不是他回家blade.....
我该如何解决这个问题?
您遇到此错误:
$count_apps is undefined
因为当您调用这条路线时:
Route::get('/', function () {
return view('home');
})->middleware('auth');
并且您没有发送 必要的数据,在这种情况下 $count_apps
您应该删除对 home
view 的错误调用,并像这样定义它:
Route::get('/{name}', array(
'as' => 'home',
'uses' => 'HomeController@index')
)->where('name', '(home)?')->middleware('auth');
这将从 /
或 /home
获取 url 并将其发送到 HomeController@index
。
现在,如果您的网站不允许未经身份验证的用户访问任何路由。
您应该在控制器的构造函数中调用 auth
中间件,这将使您的路由文件更具可读性
class HomeController extends Controller
{
/**
* Instantiate a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
}
只要调用此控制器中的函数,就会调用 auth
中间件
我的 laravel web.php
Route::get('/', function () {
return view('home');
})->middleware('auth');
Route::get('/home', 'HomeController@index');
如果我的用户没有登录,这会将我的用户重定向回登录页面,而登录的用户会重定向到主页。
现在在我的家庭控制器索引函数中,我有以下代码,
public function index()
{
$get_customers = User::where('user_roles','=','customer')->get();
$count_customers = $get_customers->count();
$get_apps = Website::all();
$count_apps = $get_apps->count();
return view('home',compact('count_customers','count_apps'));
}
每次登录后尝试访问我的主页时,我都会收到一条错误消息
$count_apps is undefined
但是,
当我在我的 web.php
中使用以下路由而不是以前的路由时,主页没有错误并且工作正常
Route::get('/', function () {
return view('auth.login');
})
但是即使这使我的登录 blade 成为索引页面,每次当我尝试以已经登录的用户身份访问索引时,它都会将我重定向到登录 blade 而不是他回家blade.....
我该如何解决这个问题?
您遇到此错误:
$count_apps is undefined
因为当您调用这条路线时:
Route::get('/', function () {
return view('home');
})->middleware('auth');
并且您没有发送 必要的数据,在这种情况下 $count_apps
您应该删除对 home
view 的错误调用,并像这样定义它:
Route::get('/{name}', array(
'as' => 'home',
'uses' => 'HomeController@index')
)->where('name', '(home)?')->middleware('auth');
这将从 /
或 /home
获取 url 并将其发送到 HomeController@index
。
现在,如果您的网站不允许未经身份验证的用户访问任何路由。
您应该在控制器的构造函数中调用 auth
中间件,这将使您的路由文件更具可读性
class HomeController extends Controller
{
/**
* Instantiate a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
}
只要调用此控制器中的函数,就会调用 auth
中间件