简体   繁体   English

模型未传递给控制器​​laravel

[英]Model not being passed to controller laravel

I've been learning Laravel and am trying to get a form in the login view to bind to a model and then have the model passed to a controller, I'm then passing the model to another the dashboard view to display the username attribute of it to test if it is working. 我一直在学习Laravel,并试图在登录视图中获取一个表单以绑定到模型,然后将模型传递给控制器​​,然后将模型传递给另一个仪表板视图以显示的用户名属性测试它是否正常工作。 The username is not being displayed therefore the model doesn't seem to be binding. 用户名未显示,因此该模型似乎没有绑定。

I've looked through the documentation and can't figure out what else I need to do/what I have done wrong. 我已经浏览了文档,无法弄清楚我还需要做什么/做错了什么。

Thanks 谢谢

Model 模型

<?php
/**
 * Created by PhpStorm.
 * User: Theo
 * Date: 25/01/2018
 * Time: 19:35
 */

namespace App\Http\Controllers\Models;


class User
{
    public $id;
    public $username;
    public $password;
}

Controller 控制者

    <?php

    namespace App\Http\Controllers;

    use App\Http\Controllers\Models\User;
    use Illuminate\Http\Request;


    class LoginController extends Controller
    {
        public function index(){
            $user = new User();
            return view('pages.login.index')->with('user', $user);
        }

        public function attemptLogin(User $user){

            return view('pages.dashboard.index')->with('user', $user);
        }
    }

Login view 登录视图

@extends('layouts.default')
@section('content')
    <div id="login-container">
      <div class="login-box">
        {!! Form::model($user, ['action' => 'LoginController@attemptLogin']) !!}

          {!! Form::text('username', @$user->username) !!}
          {!! Form::password('password', @$user->password) !!}

          {!! Form::submit('Login') !!}

          {!! Form::close() !!}
      </div>
    </div>
@stop

Dashboard view 仪表板视图

@extends('layouts.default')
@section('content')
    <h3>Welcome to the dashboard! {{{ $user->username }}}</h3>
@stop

Routes 路线

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

/*
Route::get('/players/{uid}', function ($uid) {
    return view('players' . $uid);
});
*/

Route::get('dashboard', 'DashboardController@index');

Route::get('login', 'LoginController@index');
Route::post('login/attemptLogin', 'LoginController@attemptLogin');

change 更改

<?php
/**
 * Created by PhpStorm.
 * User: Theo
 * Date: 25/01/2018
 * Time: 19:35
 */


namespace App\Http\Controllers\Models;


class User
{
    public $id;
    public $username;
    public $password;
}

to this. 为此。 put the file into app folder and add extends Model 将文件放入应用文件夹并添加扩展模型

<?php
namespace App;
/**
 * Created by PhpStorm.
 * User: Theo
 * Date: 25/01/2018
 * Time: 19:35
 */

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $fillable = ['username'];

    protected $hidden = ['password'];
}

You should create model using artisan command line by typing php artisan make:model User which will then create a model User for you in app/ folder. 您应该使用artisan命令行输入php artisan make:model User来创建模型,然后在app/文件夹中为您创建模型User

You are creating model inside app/Http/Controllers which is used for storing Controllers only. 您正在app/Http/Controllers内部创建模型,该模型仅用于存储Controller。

If you want a basic auth system you can run php artisan make:auth which will genrate a bootstrap boilerplate with basic auth system 如果您需要基本的身份验证系统,则可以运行php artisan make:auth ,它将使用基本的身份验证系统生成引导程序样板

You don't have any route parameters. 您没有任何路由参数。

In order to use Route-Model Binding, you need to either use a resource route or specify a route parameter equal to the controller method argument's name. 为了使用Route-Model Binding,您需要使用资源路由或指定与控制器方法参数名称相同的路由参数。

So if you have: 因此,如果您有:

    public function attemptLogin(User $user) {}

To have an instance of User injected, you need to have a route parameter that matches $user: 要注入User实例,您需要具有与$ user匹配的route参数:

    Route::post('login/attemptLogin/{user}', 'LoginController@attemptLogin');

However, I'm not exactly sure why you'd be trying to attempt a login into a specific user rather than provide the username as a request variable. 但是,我不确定为什么您要尝试登录到特定用户而不是提供用户名作为请求变量。

If you change the namespace of your User model to App\\Http\\Controllers\\Models\\User you need to update the config/auth.php file. 如果将用户模型的名称空间更改为App\\Http\\Controllers\\Models\\User ,则需要更新config/auth.php文件。

Search for this code: 搜索此代码:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
],

and change the model section to: 'model' => App\\Http\\Controllers\\Models\\User::class, 并将模型部分更改为: 'model' => App\\Http\\Controllers\\Models\\User::class,

BTW: Its not a good idea moving your Models below the Http folder because you mixing the HTTP layer with your business layer. 顺便说一句:将模型移到Http文件夹下不是一个好主意,因为您将HTTP层与业务层混合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM