Home > Enterprise >  My web routes request are not coming to controller
My web routes request are not coming to controller

Time:09-21

I am very new to the laravel web side, i have one page (/) after hitting this route in web it's displaying sign-in page when i fill data and submit register button the request is not coming to my controller, i could not able to figure out why it's happening like this can anyone please suggest me for approach..?

web.php

Route::post('register',[UserController::class,'register']);

Route::get('/', function () {
    return view('sign-in');
});

sign-in.blade.php

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  font-family: Arial, Helvetica, sans-serif;
  background-color: black;
}

* {
  box-sizing: border-box;
}

/* Add padding to containers */
.container {
  padding: 16px;
  background-color: white;
}

/* Full-width input fields */
input[type=text], input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  display: inline-block;
  border: none;
  background: #f1f1f1;
}

input[type=text]:focus, input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}

/* Overwrite default styles of hr */
hr {
  border: 1px solid #f1f1f1;
  margin-bottom: 25px;
}

/* Set a style for the submit button */
.registerbtn {
  background-color: #04AA6D;
  color: white;
  padding: 16px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
  opacity: 0.9;
}

.registerbtn:hover {
  opacity: 1;
}

/* Add a blue text color to links */
a {
  color: dodgerblue;
}

/* Set a grey background color and center the text of the "sign in" section */
.signin {
  background-color: #f1f1f1;
  text-align: center;
}
</style>
</head>
<body>

<form action="/register" method="POST">
  <div >
    <h1>Register</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>

    <label for="name"><b>Name</b></label>
    <input type="text" placeholder="Enter name" name="name" id="name" required>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" id="email" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" id="psw" required>


    <hr>
    <p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>

    <button type="submit" >Register</button>
  </div>

  <div >
    <p>Already have an account? <a href="#">Sign in</a>.</p>
  </div>
</form>

</body>
</html>

UserController.php

 protected $request;
    public function __construct(UserRequest $request)
    {
        $this->request = $request;
    }
public function register(UserRepository $repository){
        $data = $this->request;
        $repository->create([$this->request]);
    }

CodePudding user response:

You have to include your UserController controller on your web.php file.

<?php
  
  use App\Http\Controllers\UserController.php;

?>

CodePudding user response:

My first guess without diving in too deep is that you are missing the

@csrf

in your POST form. Laravel requires a @csrf token bit included whenever including a non-laravel form.

You can check you are reaching the endpoint by including a

dd($this->request->all()); // or just dd($this->request);

to the top of your register endpoint.

If the request still isn't making it to the backend, make sure your custom request (UserRequest) rules are fulfilled (you didn't include this code so I don't know if all of the input you are submitting are valid in relation to the rules of the request or not)

Side note: It's good practice to name your routes:

Route::post('register',[UserController::class,'register'])->name('your_route_name_here');

And then reference the route name in your form this way:

<form action="{{ route('your_route_name_here') }}" method="POST">
     @csrf
     <!-- Your form here--->
</form>

CodePudding user response:

Install laravel/ui and take inspiration from the code provided by Laravel

  • Related