我正在测试流明新的laravel微框架词,当我试图从网络服务获取输入时我得到了这个错误
Fatal error: Class 'App\Http\Controllers\Input' not found in C:\wamp\www\lumen\app\Http\Controllers\WsController.php
这是我的代码:
<?php namespace App\Http\Controllers;
class WsController extends Controller {
public function addUser()
{
$user = new Models\User;
$user->email = Input::get('email');;
$user->password = strtoupper(md5(Input::get('password')));
$user->first_name = Input::get('first_name');
$user->last_name = Input::get('last_name');
if($user->save()){
return Models\User::all();
}
}
}
答案 0 :(得分:7)
在$app->withFacades();
中取消注释bootstrap/app.php
,以允许您在控制器中使用Facade。
<?php
namespace App\Http\Controllers;
use Input;
class WsController extends Controller
{
public function addUser(Request $request)
{
$user = new Models\User;
$user->email = Input::get('email');
$user->password = strtoupper(md5(Input::get('password')));
$user->first_name = Input::get('first_name');
$user->last_name = Input::get('last_name');
if($user->save()) {
return Models\User::all();
}
}
}
使用依赖注入将Illuminate\Http\Request
类注入到方法中。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class WsController extends Controller
{
public function addUser(Request $request)
{
$user = new Models\User;
$user->email = $request->input('email');;
$user->password = strtoupper(md5($request->input('password')));
$user->first_name = $request->input('first_name');
$user->last_name = $request->input('last_name');
if($user->save()) {
return Models\User::all();
}
}
}
答案 1 :(得分:-1)
<?php namespace App\Http\Controllers;
use Illuminate\Support\Facades\Request as Request;
class WsController extends Controller {
public function addUser()
{
//creating a new user object from User model class
$user = new Models\User;
//getting the email value from the email input like Input::get('email');
$user->email = Request::input('email');
//same as above happen with the rest of the lines
$user->password = strtoupper(md5(Request::input('password')));
$user->first_name = Request::input('first_name');
$user->last_name = Request::input('last_name');
//User model has a save() method, so is calling if return true the if block will be executed
if($user->save()){
return Models\User::all();
}
}
//this is the best way in lumen to execute some method, because lumen is powerful in small APIs and restful services
}