简体   繁体   English

Laravel 5.2 - 中间件身份验证

[英]Laravel 5.2 - middleware auth

i just installed laravel 5.2, and i created auth register, login, and reset password, but now i want create a index of my project where all user (also not logged) can access.我刚刚安装了 Laravel 5.2,并创建了身份验证注册、登录和重置密码,但现在我想创建我的项目的索引,所有用户(也未登录)都可以访问该索引。 i tryed to create我试着创造

Route::get('/',HomeController@home'); Route::get('/',HomeController@home');

But this view is enable only for users logged.但此视图仅对登录的用户启用。

MY ROUTES我的路线

Route::auth();
Route::get('/home', 'HomeController@index');
// POST - FORM CREA 
Route::get('/crea-regalo', 'PostController@form');
Route::post('/crea-regalo', 'PostController@creaPost');
// LISTA ANNUNCI PRINCIPALE
Route::get('/', 'HomeController@home');

MY HOME CONTROLLER我的家庭控制器

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::orderBy('id','DESC');
        return view('home', compact('posts'));
    }

    public function home()
    {
        $posts = Post::all();
        return view('index', compact('posts'));
    }
}

How can i create routes of view where ALL users can access?如何创建所有用户都可以访问的视图路径?

Thank you for your help!感谢您的帮助!

Hi write separate controller to access page to all because you have written auth middleware in contructor嗨,编写单独的控制器来访问所有人的页面,因为您已经在构造函数中编写了身份验证中间件

public function __construct()
{
   $this->middleware('auth');
}

Similar like类似的喜欢

class GuestController extends Controller
{

    public function __construct()
    {

    }


    public function home()
    {
        $posts = Post::all();
        return view('index', compact('posts'));
    }
}

In route途中

Route::get('/home', 'GuestController@home');

or else you can do like this否则你可以这样做

$this->middleware('auth', ['except' => ['home']]);

this will able to access home page for all .In your constructor add this这将能够访问所有人的主页。在你的构造函数中添加这个

public function __construct()
{
   $this->middleware('auth', ['except' => ['home']]);
}

Put those route which you want to allow only authenticated user in middleware auth as follows:将您希望仅允许经过身份验证的用户的路由放入中间件身份验证中,如下所示:

Route::group(['middleware' => ['auth']], function () {
  //your routes    
})

And for those routes which all user can access put that out side the above group.对于所有用户都可以访问的路由,将其放在上述组之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM