数据未传递到视图中

时间:2020-05-19 03:31:14

标签: php laravel

我试图在laravel中添加忘记密码的功能,当我单击“重置密码”链接时,它显示错误“未定义的变量:标题(视图:)。我已经在控制器中定义了标题,但仍然显示此错误。似乎是传递的数据在视图中不可用,但在忘记的视图中可用。

我的ResetPasswordController.php是

class ResetPasswordController extends Controller
{

    public function __construct()
    {

    }

    public function reset(Request $request) {
        $data['title'] = "Reset";

        return view('auth.passwords.reset',$data);
    }  

}

忘记密码可以正常工作,该密码具有相同的@ layout.app且具有此“ title”变量,但“重置”无效。以下是路线代码

Route::get('/password/forgot', [ 'as' => 'forgot', 'uses' => 'ForgotPasswordController@reset']);
Route::get('/password/reset', [ 'as' => 'reset', 'uses' => 'ResetPasswordController@reset']);

视图为

<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ $title }}</title>
    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}" defer></script>
    <!-- Fonts -->
    <link rel="dns-prefetch" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css?family=Raleway:300,400,600" rel="stylesheet" type="text/css">
    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
    <div id="app">
        <nav class="navbar navbar-expand-md navbar-light navbar-laravel">
            <div class="container"> 
                <a class="navbar-brand" href="{{ url('/') }}">
                    {{Config::get('constants.APP_TITLE')}}
                </a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>


                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <!-- Left Side Of Navbar -->
                    <ul class="navbar-nav mr-auto"></ul>


                    <!-- Right Side Of Navbar -->
                    <ul class="navbar-nav ml-auto">
                        <!-- Authentication Links -->
                        @guest
                            <li><a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a></li>
                            <li><a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a></li>
                        @else
                            <li><a class="nav-link" href="{{ route('users.index') }}">Manage Users</a></li>
                            <li><a class="nav-link" href="{{ route('roles.index') }}">Manage Role</a></li>
                            <li><a class="nav-link" href="{{ route('products.index') }}">Manage Product</a></li>
                            <li class="nav-item dropdown">
                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                    {{ Auth::user()->name }} <span class="caret"></span>
                                </a>


                                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                    <a class="dropdown-item" href="{{ route('logout') }}"
                                       onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                        {{ __('Logout') }}
                                    </a>


                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                        @csrf
                                    </form>
                                </div>
                            </li>
                        @endguest
                    </ul>
                </div>
            </div>
        </nav>


        <main class="py-4">
            <div class="container">
            @yield('content')
            </div>
        </main>
    </div>
</body>
</html>

此功能在“忘记密码”视图中有效,但在“重置密码”视图中出现错误

我也尝试过

$title = "Reset";
    return view('auth.passwords.reset')->with('title', $title);

2 个答案:

答案 0 :(得分:0)

您必须像这样使用

return view('auth.passwords.reset',compact('data'));

答案 1 :(得分:0)

您可以像A.A.Noman的答案一样将变量传递到视图中,否则您可以这样做。

return view('auth.passwords.reset',['data' => $data] );