dee*_*ngh 10 php laravel laravel-routing laravel-5
我的项目中有很多php文件:
admin.blade.php:此文件包含管理表单.
调用时显示以下错误:
RouteCollection.php第201行中的MethodNotAllowedHttpException
<h2>Please Log In To Manage</h2>
<form id="form1" name="form1" method="post" action="<?=URL::to('/admin')?>">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
User Name:<br />
<input name="username" type="text" id="username" size="40" />
<br /><br />
Password:<br />
<input name="password" type="password" id="password" size="40" />
<br />
<br />
<br />
<input type="submit" name="button" id="button" value="Log In" />
</form>
Run Code Online (Sandbox Code Playgroud)
在route.php,此调用是:
Route::get('/admin',array('uses'=>'student@admin'));
Run Code Online (Sandbox Code Playgroud)
这是函数 student.php
public function admin()
{
return View::make('student.admin');
$validator = Validator::make($data = Input::all() , User::rules());
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
else
{
$check = 0;
$check = DB::table('admin')->get();
$username = Input::get('username');
$password = Input::get('password');
if (Auth::attempt(['username' => $username, 'password' => $password]))
{
return Redirect::intended('/');
}
return Redirect::back()->withInput()->withErrors('That username/password combo does not exist.');
}
}
Run Code Online (Sandbox Code Playgroud)
我不太了解创建管理区域,我只是想创建它.
我就是这样做的.
routes.php文件
Route::get('/admin', 'UsersController@getAdminLogin');
Route::get('/admin/dashboard', 'UsersController@dashboard');
Route::post('/admin', 'UsersController@postAdminLogin');
Run Code Online (Sandbox Code Playgroud)
admin_login.blade.php
{!! Form::open(['url' => '/admin']) !!}
<div class="form-group">
{!! Form::label('email', 'Email Id:') !!}
{!! Form::text('email', null, ['class' => 'form-control input-sm']) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', ['class' => 'form-control input-sm']) !!}
</div>
<div class="form-group">
{!! Form::submit('Login', ['class' => 'btn btn-primary btn-block']) !!}
</div>
{!! Form::close() !!}
Run Code Online (Sandbox Code Playgroud)
dashboard.blade.php
<h4 class="text-center">
Welcome {{ Auth::user()->full_name }}
</h4>
Run Code Online (Sandbox Code Playgroud)
UsersController.php
/**
* Display the admin login form if not logged in,
* else redirect him/her to the admin dashboard.
*
*/
public function getAdminLogin()
{
if(Auth::check() && Auth::user()->role === 'admin')
{
return redirect('/admin/dashboard');
}
return view('admin_login');
}
/**
* Process the login form submitted, check for the
* admin credentials in the users table. If match found,
* redirect him/her to the admin dashboard, else, display
* the error message.
*
*/
public function postAdminLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email|exists:users,email,role,admin',
'password' => 'required'
]);
$credentials = $request->only( 'email', 'password' );
if(Auth::attempt($credentials))
{
return redirect('/admin/dashboard');
}
else
{
// Your logic of invalid credentials.
return 'Invalid Credentials';
}
}
/**
* Display the dashboard to the admin if logged in, else,
* redirect him/her to the admin login form.
*
*/
public function dashboard()
{
if(Auth::check() && Auth::user()->role === 'admin')
{
return view('admin.dashboard');
}
return redirect('/admin');
}
Run Code Online (Sandbox Code Playgroud)
你的代码:
在routes.php,你只有1条路线,即
Route::get('/admin',array('uses'=>'student@admin'));
Run Code Online (Sandbox Code Playgroud)
并且没有post方法的声明,因此,MethodNotAllowedHttpException
此外,在您的控制器中,您首先返回视图,然后处理根本不起作用的表单.首先需要处理表单然后返回视图.
public function admin(){
// Won't work as you are already returning the view
// before processing the admin form.
return \View::make(students.admin);
// ...
}
Run Code Online (Sandbox Code Playgroud)
正如@Sulthan建议的那样,你应该使用Form Facade.你可以看看这个视频在Laracasts什么Form Facade是你应该如何使用它.
您post在表单中使用方法,但get在路由中使用方法。
因此,将方法更改为post您的路线
注意 :
我建议您使用Laravel的默认表单开头,如下所示,这始终是最佳做法
{!! Form::open(array('url' => 'foo/bar')) !!}
{!! Form::close() !!}
Run Code Online (Sandbox Code Playgroud)
提示 :
在此处阅读更多内容,并尝试通过比较方法和路由来调试此类内容。
默认情况下,laravel 5中不包含Form Facade。您应通过以下方式安装
composer require "illuminate/html":"5.0.*"
Run Code Online (Sandbox Code Playgroud)
并在app.php中进行更新。
我写了一个博客,其中简要介绍了此安装。
| 归档时间: |
|
| 查看次数: |
63058 次 |
| 最近记录: |