获取用户名时显示错误

时间:2017-06-15 08:39:08

标签: laravel lumen

显示错误:App \ Http \ Controllers \ AdminLoginController :: name()缺少参数1

 public function name($username) {
            $user = AdminLogin::find($username);
            return response()->json($user);
        }

AdminLoginController:它是一个adminlogin控制器代码

class AdminLoginController extends Controller{

public function show(){

        $res ="Hello world!"; 

        return response()->json($res);
    }

     public function log() {

        $users = AdminLogin::all();

        return response()->json($users);
    }    
    public function name($username) {
    $user = AdminLogin::where('username',$username)->first();
    return response()->json($user);
    } 

RouteLoginController:它是一个adminlogin控制器代码:

<?php

$app->get('/', function () use ($app) {
    return $app->version();
});

$app->group(['prefix' => 'api/v1'], function ($app)
{
    $app->get('adminlogin', 'AdminLoginController@show'); //get single route
    $app->get('user', 'AdminLoginController@log'); //get single route
    $app->get('username', 'AdminLoginController@name'); //get single route
    $app->post('adminlogin', 'AdminLoginController@login'); //get single route

});

错误:

(1/1)ErrorException 缺少App \ Http \ Controllers \ AdminLoginController :: name()

的参数1

3 个答案:

答案 0 :(得分:0)

您可能正在使用ajax请求调用此函数并将该名称放在查询字符串中。在这种情况下,name参数不会作为函数的属性发送,但会成为请求对象的一部分。

你可以这样解决这个问题:

public function name(\Illuminate\Http\Request $request) {
    $user = AdminLogin::find($request->username);
    return response()->json($user);
} 

答案 1 :(得分:0)

You should try this :

public function name($username) {
    $user = AdminLogin::where('username',$username)->first();
    return response()->json(['user' => $user]);
} 

OR

public function name(\Illuminate\Http\Request $request) {
    $user = AdminLogin::where('username',$request->username)->first();
    return response()->json(['user' => $user]);
} 

答案 2 :(得分:0)

您的控制器方法正在使用用户名参数,但路由绑定未通过。改变你的路线

$app->get('username', 'AdminLoginController@name');

$app->get('user/{username}', 'AdminLoginController@name');

如果您不想更改路线,请将控制器功能签名更改为以下内容(如其他答案所示),并确保您传递的用户名为&#39;在调用url时作为请求参数。

public function name(\Illuminate\Http\Request $request) {
    $user = AdminLogin::where('username',$request->username)->first();
    return response()->json(['user' => $user]);
}