简体   繁体   中英

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. i tryed to create

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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