“找不到类'App \ Controllers \ controller'”

时间:2019-05-01 03:50:16

标签: laravel

我是laravel的新手,我正在尝试制作自己的登录和注册表单,以便用户可以登录和注册,我的注册表单可以正常工作,它可以收集数据并将其存储在数据库中,但是每当我尝试使用相同的数据,它也给我这个错误。我可以尝试多种方法,但失败了。 谢谢。...

错误: 找不到“ App \ Controllers \ controller”类

我的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

控制器:

<?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();
    }
}

路线:

<?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'=>'UserController@postSignUp',
    'as'=>'signup'

]);


Route::post('/signin',[
    'uses'=>'UserController@postSignIn',
    'as'=>'signin'

]);

Route::get('/dashboard',[
    'uses'=>'UserController@getdashboard',
    'as'=>'dashboard'

]);


Auth::routes();

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

4 个答案:

答案 0 :(得分:3)

您的名称空间对于访问Controllers文件夹是错误的,控制器文件上的代码的第一行更改了:

namespace App\Controllers;

namespace App\Http\Controllers;

如果有此错误(Cannot use object of type Illuminate\Support\Facades\Request as array),也请更改此部分:

use Request;  to  use Illuminate\Http\Request;

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

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

答案 1 :(得分:0)

默认情况下,laravel将控制器放在

app\Http\Controllers

因此,将namespace App\Controllers;替换为namespace App\Http\Controllers;

编辑:

访问请求变量,如下所示:

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

答案 2 :(得分:0)

$request是一个对象,因此您可以使用->获取属性。如下更改控制器代码:

<?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();
    }
}

答案 3 :(得分:0)

我想在此处链接一些laravel usage samples,并且在Controller有一个名为App\Http\Controllers\Controller的类,您需要将其用作基类。 ,但总之,只需将控制器名称空间从App\Controllers更改为App\Http\Controllers,或者甚至可以为自己实现Controller类,如下所示:

<?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);
    }
}
  

请注意,PHP对于类命名不区分大小写,但是您可以使用和扩展\App\Http\Controllers\Controller而不是仅仅controller