5

嗨,我正在测试 lumen new laravel 微框架字,当我尝试从 Web 服务获取输入时,我收到了这个错误

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

2 回答 2

7

解决方案#1

取消注释$app->withFacades();bootstrap/app.php允许您在控制器中使用外观。

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

解决方案#2(个人喜好)

使用依赖注入将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();
        }
    }
}
于 2015-04-29T11:34:40.353 回答
-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
    }
于 2015-04-30T05:46:18.383 回答