fahico98
fahico98

Reputation: 162

Middleware 'auth' does not work in Laravel 5.8

Apologize, I'm latin and my english is not good.

I'm using the auth middlewate in UserController.php file but does not work, when I try to access user/profile_picture and user/bio routes from no logged in user Laravel throws an The GET method is not supported for this route. Supported methods: POST. exception.

web.php:

Route::get("/", function(){
   return view('welcome');
});

Route::get('home', 'HomeController@index')->name('home');

Route::get("admin/users/seller_register", "Auth\RegisterController@showSellerRegistrationForm")
   ->name("users.seller_register_form");
Route::post("admin/users/seller_register", "Auth\RegisterController@sellerRegister")
   ->name("users.seller_register");

Route::get("admin/users/modal_delete_form", "AdministratorController@modalDeleteForm");
Route::get("admin/users/modal_update_form", "AdministratorController@modalUpdateForm");
Route::get("admin/users/crud_content", "AdministratorController@crudContent");
Route::resource('admin/users', 'AdministratorController');

Route::get("user/profile/{e_mail}", "UserController@profile")->name("user.profile");
Route::post("user/profile_picture", "UserController@profilePicture")->name("user.profilePicture");
Route::post("user/bio", "UserController@bio")->name("user.bio");

Auth::routes(["verify" => true]);

UserController.php:

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use App\User;

class UserController extends Controller{

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

   /**
    * Display the user profile.
    *
    * @param  String  $e_mail
    * @return \Illuminate\Http\Response
    */
   public function profile($e_mail){
      $user = User::where("e_mail", "=", $e_mail)->first();
      return(view("user.profile")->with(["user" => $user]));
   }

   /**
    * Store the user profile picture.
    *
    * @param  \Illuminate\Http\Request
    * @return \Illuminate\Http\Response
    */
   public function profilePicture(Request $request){
      $user = User::where("e_mail", $request->e_mail)->first();
      if($request){
         if($request->hasFile("profilePicture")){
            if($user->profile_picture === "public/defaultUserPhoto.jpg"){
               $path = Storage::putFile('public', $request->file('profilePicture'));
            }else{
               Storage::delete($user->profile_picture);
               $path = Storage::putFile("public", $request->file('profilePicture'));
            }
            $user->update(["profile_picture" => $path]);
         }
      }
      return(redirect()->route("user.profile", ["e_mail" => $user->e_mail]));
   }

   /**
    * Update user biography and occupation.
    *
    * @param  \Illuminate\Http\Request
    */
   public function bio(Request $request){
      $user = User::where("e_mail", $request->e_mail)->first();
      if($request){
         $user->update([
            "occupation" => $request->occupation,
            "biography" => $request->biography
         ]);
      }
      return(redirect()->route("user.profile", ["e_mail" => $user->e_mail]));
   }
}

Upvotes: 1

Views: 270

Answers (2)

Md Al imrun Khandakar
Md Al imrun Khandakar

Reputation: 159

Just change these routes

Route::post("user/profile_picture", "UserController@profilePicture")->name("user.profilePicture");
Route::post("user/bio", "UserController@bio")->name("user.bio");

to...

Route::get("user/profile_picture", "UserController@profilePicture")->name("user.profilePicture");
Route::get("user/bio", "UserController@bio")->name("user.bio");

done.

Upvotes: 1

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

Try this use get method

Route::get("user/profile_picture","UserController@profilePicture")->name("user.profilePicture");

Route::get("user/bio", "UserController@bio")->name("user.bio");

Upvotes: 1

Related Questions