Laravel 5.2 $ this->请求为空

时间:2016-11-11 13:58:20

标签: php laravel request laravel-5.2 httprequest

我有以下代码,我尝试通过laravel中的路线进行后期调用:

routes.php文件

use Illuminate\Support\Facades\Input;
Route::post(config('api.basepath') . 'users/parent', array(
    'uses' => 'UserAccountController@createParent'
));

createAarent()方法在UserAccount Controller =>

中如下所示

UserAccountController.php

namespace App\Http\Controllers;

use Auth;
use Mail;
use Lang;
use Validator;
use Hash;
use Storage;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Log;

class UserAccountController extends Controller
{

    public function createParent()
    {
        Log::info($this->request);
        $this->verify($this->request, 'validations.user.create');
        ......
        ......
    }
}

这是输出:

[2016-11-11 13:43:06] local.INFO: null 

#0 /home/test/myproject/web/app/Http/Controllers/UserAccountController.php(268): App\Http\Controllers\Controller->verify(NULL, 'validations.use...')
#1 [internal function]: App\Http\Controllers\UserAccountController->createParent()
#2 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(80): call_user_func_array(Array, Array)
#3 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(146): Illuminate\Routing\Controller->callAction('createParent', Array)
#4 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(94): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\UserAccountController), Object(Illuminate\Routing\Route), 'createParent')
#5 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#6 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#7 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#8 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#9 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(96): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#10 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(54): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\UserAccountController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'createParent')
#11 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Route.php(174): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\\Http\\Contro...', 'createParent')
#12 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Route.php(140): Illuminate\Routing\Route->runController(Object(Illuminate\Http\Request))
#13 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(724): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#14 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#15 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#16 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#17 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#18 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(726): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#19 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(699): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#20 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#21 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#22 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#23 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#24 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#25 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#26 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#27 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#29 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#30 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#31 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#32 /home/test/myproject/web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#33 /home/test/myproject/web/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#34 {main}  

请帮助。我现在被困在这几个小时了。

3 个答案:

答案 0 :(得分:1)

你的功能应该是:

public function createParent(Request $request)
{
    Log::info($request);
    $this->verify($request, 'validations.user.create');
    ......
    ......
}

并确保将控制器顶部的Illuminate\Http\Request类导入为:

use Illuminate\Http\Request;

答案 1 :(得分:0)

您从未在方法createParent()中传递请求类对象。

试试这个

public function createParent(Request $request) 
{
    $inputs = $request->all(); 
}

答案 2 :(得分:0)

我终于开始工作了。 这是一个代码问题。

修改

实际上它有一个问题,因为在我的课上,我使用了另一个类,并初始化了它的对象。但是这个类构造函数反过来又期望它包含在它的类中的进一步的类初始化。所以我必须初始化这些子依赖类,然后初始化类对象。