1

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!

2 Answers 2

3

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']]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

yup! thank you! i will create another controller, is the best choice!
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.