laravel 5 : Class 'input' not found

2019-01-30 02:36发布

In my routes.php file I have :

Route::get('/', function () {

    return view('login');
});

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

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

    $user = new \App\User;
    $user->username = input::get('username');
    $user->email  = input::get('email');
    $user->password = Hash::make(input::get('username'));
    $user->designation = input::get('designation');
    $user->save();

});

I have a form for users registration. I am also taking the form inputs value in the routes.php.

But the error comes up when I register a user . Error:

FatalErrorException in routes.php line 61:
Class 'input' not found

11条回答
Explosion°爆炸
2楼-- · 2019-01-30 03:07

if You use Laravel version 5.2 Review this: https://laravel.com/docs/5.2/requests#accessing-the-request

use Illuminate\Http\Request;//Access able for All requests
...

class myController extends Controller{
   public function myfunction(Request $request){
     $name = $request->input('username');
   }
 }
查看更多
叛逆
3楼-- · 2019-01-30 03:12

Add this in config/app.php under aliases:-

'Input' => Illuminate\Support\Facades\Input::class,
查看更多
女痞
4楼-- · 2019-01-30 03:13

In Laravel 5.2 Input:: is replaced with Request::

So where ever you need to input something instead of using

Input:: 

use

Request::

And if you get error something about 'should not use statically' just add this at the top of your file

use Request;

If you already have this line:

use Illuminate\Http\Request;

delete it because you can't have two classes with the same name in one file

查看更多
淡お忘
5楼-- · 2019-01-30 03:16

You can add a facade in your folder\config\app.php

'Input' => Illuminate\Support\Facades\Input::class,
查看更多
我想做一个坏孩纸
6楼-- · 2019-01-30 03:19

This clean code snippet works fine for me:

use Illuminate\Http\Request;
Route::post('/register',function(Request $request){

$user = new \App\User;
$user->username = $request->input('username');
$user->email  = $request->input('email');
$user->password = Hash::make($request->input('username'));
$user->designation = $request->input('designation');
$user->save();

});

查看更多
时光不老,我们不散
7楼-- · 2019-01-30 03:21

In first your problem is about the spelling of the input class, should be Input instead of input. And you have to import the class with the good namespace.

use Illuminate\Support\Facades\Input;

If you want it called 'input' not 'Input', add this :

use Illuminate\Support\Facades\Input as input;

Second, It's a dirty way to store into the database via route.php, and you're not processing data validation. If a sent parameter isn't what you expected, maybe an SQL error will appear, its caused by the data type. You should use controller to interact with information and store via the model in the controller method.

The route.php file handles routing. It is designed to make the link between the controller and the asked route.

To learn about controller, middleware, model, service ... http://laravel.com/docs/5.1/

If you need some more information, solution about problem you can join the community : https://laracasts.com/

Regards.

查看更多
登录 后发表回答