我所做的是非常基本的。我可以在重定向之前看到我的验证错误,所以我认为我已经覆盖了会话(我没有做过)或从视图中删除某些内容,因为验证器工作也没有意义对于我的\ Auth \ register但没有其他动作。
这就是我所拥有的:
在\ App \ Controllers \ QuestionsController
中QuestionsController extends Controller{
protected$rules=[
'question'=>array(
'text'=>'required|max:2000',
'categories'=>'required|min:4'
)
];
public function create(Request $request){
$valid=Validator::make($request->all(),$this->rules['question']);
if($valid->fails()){
var_dump($valid->messages());//exit;
return redirect('/questions')->withErrors($valid,'questions');
}
//...additional adding scripts etc...
}
现在这是重定向之前我可以看到我的消息!
现在你觉得我很傻,没有使用名字包或其他东西,但即使我删除名字包,我也看不到任何消息:S
在我的问题中。添加视图
@import('defaults.errors',['sub'=>'questions'])
{{var_dump(count($errors))}}
{{var_dump($errors->questions)}}
in defaults.errors
@if(isset($sub))
@if(count($errors->$sub)>0)
<div class="alert alert-danger">
<ul class='nav'>
@foreach($errors->$sub->all() as $error)
<li><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> {{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@else
@if(count($errors)>0)
<div class="alert alert-danger">
<ul class='nav'>
@foreach($errors->all() as $error)
<li><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> {{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@endif
我想也许是因为我不太了解Laravel,也许我已经覆盖了&#39;与我的控制器中的$ errors或session有关?
这就是我的控制器的样子
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Auth;
use App\conversation;
use App\notifications;
use App\relationships;
use App\Helpers\DatetimeHelper;
use Azza\Utilities\NumberSuffix;
class Controller extends BaseController
{
public $user;
public $title=array('title');
public $titleSeperator=' - ';
public $keywords=array('hello','I added');
public $messageCount=0;
public$notifications=null;
public$notificationsCount=0;
public$friendRequests=0;
public$bodyClass=array();
use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
public function __construct(){
$this->user=Auth::user();
if($this->user){
$this->messageCount=conversation::getByUserId($this->user->id)->countUnseen()->count();
//$this->notifications=notifications::getByUserId($this->user->id)->get()->paginate(5);
$this->notificationsCount=notifications::getByUserId($this->user->id)->countUnseen()->count();
//var_dump($this->messageCount->count());exit;
$this->friendRequests=relationships::countRequests($this->user->id)->count();
}
view()->composer('*',function($view){
$view->with('currentUser',$this->user);
$view->with('title',$this->title);
$view->with('titleSeperator',$this->titleSeperator);
$view->with('keywords',$this->keywords);
$view->with('messageCount',$this->messageCount);
$view->with('notificationsCount',$this->notificationsCount);
$view->with('friendRequests',$this->friendRequests);
$view->with('DatetimeHelper',new DatetimeHelper());
$view->with('NumberSuffix',new NumberSuffix());
$view->with('bodyClass',implode(' ',$this->bodyClass));
});
}
public function setDefaultVals($name,$val){
view()->composer('*',function($view) use($name,$val){
$view->with($name,$val);
});
}
}
这样做的基本思想是释放时间来操纵我的头信息并使用动态数组来获取数据。我知道这看起来很混乱,可能不是最好的方式,但我现在只是搞乱了laravel。
答案 0 :(得分:1)
我已经在Laravel环境中设置了你的代码,你所做的一切都是正确的。但是,在5.2
laravel的中间件行为略有不同,您的$errors
变量仅在StartSession
中间件处于活动状态时才可用。
你的Kernel.php看起来像这样,或类似的:
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
尝试将其更改为:
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
注意\Illuminate\Session\Middleware\StartSession::class
答案 1 :(得分:0)
尝试
return redirect('/questions')->withErrors($valid->messages());