I am new to laravel here i am trying to make my own login and registration form where user can login and register,My registration forms works fine it collects the data and stores it in a database,But whenever i try to login using the same data it gives me this error.Any help would be appreciated i have tried many ways but failed. Thanks....

Error: Class 'App\Controllers\controller' not found

MY HTML:

@extends("layouts.master")

@section('title')
My page
@endsection

@section('content')
<div class="row">
<div class="col-md-6">
<h3>Sign-Up</h3>
<form action="{{route('signup')}}" method="post">

<div class="form-group">
<label for="email">Your email</label>
<input class="form-control" type="text" name="email" id="email">
</div>

<div class="form-group">
<label for="first_name">First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>

<div class="form-group">
<label for="password">Password</label>
<input class="form-control"  type="password" name="password" id="password">
</div>

<button type="sumbit" class="btn btn-primary">sumbit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">

</form>
</div>

<div class="col-md-6">
<h3>Login </h3>
<form action="{{route('signin')}}" method="post">

<div class="form-group">
<label for="email">Your email</label>
<input class="form-control" type="text" name="email" id="email">
</div>

<div class="form-group">
<label for="password">Password</label>
<input class="form-control"  type="password" name="password" id="password">
</div>

<button type="sumbit" class="btn btn-primary">sumbit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">

</form>
</div>
</div>
@endsection

CONTROLLER:

<?php
namespace App\Controllers;

use App\Http\Requests;
use App\User;
use App\UserTypes;

use Auth;
use Hashids;
use Redirect;
use Request;
use Hash;



class UserController extends controller
{
    public function getdashboard()
    {
        return view('dashboard');
    }

    public function postSignUp(Request $request)
    {
        $email = $request['email'];
        $first_name = $request['first_name'];
        $password = bcrypt($request['password']);


        $user = new User();
        $user->email = $email;
        $user->first_name = $first_name;
        $user->password = $password;

        $user->save();
        return redirect()->route('dashboard');
    }
    public function postSignIn(Request $request)
    {
        if(Auth::attempt([ 'email' => $request['email'],'password' => $request['password']])){
            return redirect()->route('dashboard');
        }
        return redirect()->back();
    }
}

ROUTE:

<?php

/*
|--------------------------------------------------------------------------
| Web 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!
|
*/

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


Route::post('/signup',[
    'uses'=>'[email protected]',
    'as'=>'signup'

]);


Route::post('/signin',[
    'uses'=>'[email protected]',
    'as'=>'signin'

]);

Route::get('/dashboard',[
    'uses'=>'[email protected]',
    'as'=>'dashboard'

]);


Auth::routes();

Route::get('/home', '[email protected]')->name('home');

4 Answers

3
Khalil DaNish On

Your name space is wrong for accessing the Controllers folder, the first line of the code on the controller file change:

namespace App\Controllers;

to

namespace App\Http\Controllers;

also change this part in case if you have this error (Cannot use object of type Illuminate\Support\Facades\Request as array):

use Request;  to  use Illuminate\Http\Request;

and

$email = $request['email'];
$first_name = $request['first_name'];
$password = bcrypt($request['password']);

to

$email = $request->email;
 $first_name = $request->first_name;
 $password = bcrypt($request->password);
0
dexter On

By default laravel places controller under

app\Http\Controllers

therefore, replace namespace App\Controllers; by namespace App\Http\Controllers;

Edit:

Access the variable of request like following:

 $email = $request->input('email');
 $first_name = $request->input('first_name');
 //similar way for other inputs
0
Rouhollah Mazarei On

The $request is an object so you can get the properties with ->. Change your controller code as below:

<?php
namespace App\Http\Controllers; // HERE

use Illuminate\Http\Request; // FOR YOUR COMMENT
use App\Http\Requests;
use App\User;
use App\UserTypes;

use Auth;
use Hashids;
use Redirect;
use Request;
use Hash;



class UserController extends controller
{
    public function getdashboard()
    {
        return view('dashboard');
    }

    public function postSignUp(Request $request)
    {
        $email = $request->email;// HERE
        $first_name = $request->first_name;// HERE
        $password = bcrypt($request->password);// HERE


        $user = new User();
        $user->email = $email;
        $user->first_name = $first_name;
        $user->password = $password;

        $user->save();
        return redirect()->route('dashboard');
    }
    public function postSignIn(Request $request)
    {
        if(Auth::attempt([ 'email' => $request->email,'password' => $request->password])){// HERE
            return redirect()->route('dashboard');
        }
        return redirect()->back();
    }
}
0
Top-Master On

There are some laravel usage samples which I would like to link here, and there is a class called Controller at App\Http\Controllers\Controller which you need to use as your base class, but in short, just change your controller namespace from App\Controllers into App\Http\Controllers, or you could even implement the Controller class for your self which could be looking something like below:

<?php
namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class UserController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function postSignUp(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email|unique:users',
            'first_name' => 'required|max:120',
            'password' => 'required|min:4'
        ]);
        $email = $request['email'];
        $first_name = $request['first_name'];
        $password = bcrypt($request['password']);
        $user = new User();
        $user->email = $email;
        $user->first_name = $first_name;
        $user->password = $password;
        $user->save();
        Auth::login($user);
        return redirect()->route('dashboard');
    }
    public function postSignIn(Request $request)
    {
        $this->validate($request, [
            'email' => 'required',
            'password' => 'required'
        ]);
        if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
            return redirect()->route('dashboard');
        }
        return redirect()->back();
    }
    public function getLogout()
    {
        Auth::logout();
        return redirect()->route('home');
    }
    public function getAccount()
    {
        return view('account', ['user' => Auth::user()]);
    }
    public function postSaveAccount(Request $request)
    {
        $this->validate($request, [
           'first_name' => 'required|max:120'
        ]);
        $user = Auth::user();
        $old_name = $user->first_name;
        $user->first_name = $request['first_name'];
        $user->update();
        $file = $request->file('image');
        $filename = $request['first_name'] . '-' . $user->id . '.jpg';
        $old_filename = $old_name . '-' . $user->id . '.jpg';
        $update = false;
        if (Storage::disk('local')->has($old_filename)) {
            $old_file = Storage::disk('local')->get($old_filename);
            Storage::disk('local')->put($filename, $old_file);
            $update = true;
        }
        if ($file) {
            Storage::disk('local')->put($filename, File::get($file));
        }
        if ($update && $old_filename !== $filename) {
            Storage::delete($old_filename);
        }
        return redirect()->route('account');
    }
    public function getUserImage($filename)
    {
        $file = Storage::disk('local')->get($filename);
        return new Response($file, 200);
    }
}

Note that PHP is case-insensitive for the class naming, but you could use and extend \App\Http\Controllers\Controller instead of just controller