Prevent my website root page redirect to login in laravel 8 -- [Question Asked]

Issue

When I log out my website root (127.0.0.1:8000) redirect to the login page (127.0.0.1:8000/login) and can’t open it.
in this link (127.0.0.1:8000) my frontend pages and must be accessed without any login
and I want to be available to all users.

The website pages that I want it to available to all called by routes bellow

Route::get('/', '[email protected]')->name('home');
Route::get('/read/{id}', '[email protected]')->name('read');
Route::post('/read/{id}', '[email protected]')->name('postread');

and my admin routes working successfully under the routes group (dashboard).

My routs file site.php

<?php
use App\Article;
use App\Http\Controllers;
/*
|--------------------------------------------------------------------------
| Site Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Auth::routes();

Route::get('/', '[email protected]')->name('home');
Route::get('/read/{id}', '[email protected]')->name('read');
Route::post('/read/{id}', '[email protected]')->name('postread');
Route::group(['prefix' => 'dashboard','middleware' => 'guest:api'], function () {
    Route::get('/', '[email protected]_index')->name('dashboard');
    Route::get('/add', '[email protected]')->name('addarticle');
    Route::post('/add', '[email protected]')->name('addpostarticle');
    Route::get('/delete/{id}','[email protected]');
    Route::post('/edit/{id}', '[email protected]')->name('edit');
    Route::get('/edit/{id}','[email protected]'); 
});

My LoginController

<?php

namespace App\Http\Controllers\Auth;
use Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = 'dashboard';
    

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
        
    }

    public function login(Request $request){

        $message = array(
            'required.email'    =>  'This is required',
            'required.password' =>  'This is required',
        );
        $this->validate($request,[
            'email' =>  'required',
            'password'  =>  'required',
        ],$message);
    
        $email = $request->email;
        $pass = $request->password;
    
        if(Auth::attempt(['email' => $email, 'password' => $pass])){
            //Session::flash('success','Welcome '.Auth::user()->name);
            return redirect()->route('dashboard');
        }else{
            Session::flash('error','Sorry! Try Again. It seems your login credential is not 
correct.');
            return redirect()->back();
        }
    
    }


}

HomeController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Article;
use App\Comment;
use Illuminate\Support\Facades\Auth;

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()
    {

        $articles = Article::all();
        $ar=Array('articles'=>$articles);
        return view('site.home',$ar);
    }


    public function admin_index()
    {

        $articles = Article::all();
        $ar=Array('articles'=>$articles);
        return view('admin.home',$ar);
    }


    
    public function AddArticle(Request $request){
        if($request ->isMethod('post')){
           $ar = new Article();
           $file_extension = $request -> image -> getClientOriginalExtension();
           $file_name = time().'.'.$file_extension;
           $path = "upload";
           $request -> image -> move($path,$file_name);
           $ar->title=$request->input('title');
           $ar->body=$request->input('body');
           $ar->image=$file_name;
           $ar->user_id=Auth::user()->id;
           $ar->save();
           return redirect("dashboard");
        }
        return view('admin.AddArticle');
    }


    public function read(Request $request,$id){
        if($request ->isMethod('post')){
            $ar = new Comment();
            $ar->Comment=$request->input('body');
            $ar->article_id=$id;
            $ar->save();
         }
        $article = Article::find($id);
        $ar=Array('article'=>$article);
        return view('site.read',$ar);
    }
    public function DeleteArticle($id){
        $article = Article::find($id);
        $article->delete();
        return redirect("dashboard");
    }
    public function Edit(Request $request,$id){
        if($request ->isMethod('post')){
            $ar = Article::find($id);

        if($request->hasFile('image')) {
            $file_extension = $request -> image -> getClientOriginalExtension();
            $file_name = time().'.'.$file_extension;
            $path = "upload";
           
                $request -> image -> move($path,$file_name);

                $ar->image=$file_name;
                }
            $ar->title=$request->input('title');
            $ar->body=$request->input('body');

            $ar->user_id=Auth::user()->id;
            
            $ar->save();
            return redirect("dashboard");
        }else{
            $article = Article::find($id);
            $ar=Array('article'=>$article);
            return view('admin.edit',$ar);
        }
    }

}

Solution

Return to login page because there is middleware

Just edit HomeController

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

->except() method will exclude functions from auth middleware

Answered By – Mohamed Gamal Eldin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Posted in PHP

Who we are?

We are team of software engineers in multiple domains like Programming and coding, Fundamentals of computer science, Design and architecture, Algorithms and data structures, Information analysis, Debugging software and Testing software. We are working on Systems developer and application developer. We are curious, methodical, rational, analytical, and logical. Some of us are also conventional, meaning we're conscientious and conservative.

Answer collected from stackoverflow and other sources, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0