我试图在Laravel中提交表单时显示成功消息。
然而,它不起作用。
我在控制器文件的顶部添加了use Session;
,路由在中间件中,config / session.php中的配置是默认配置。
我的控制器功能可以毫无问题地保存在数据库中:
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->description = $request->description;
$post->slug = $request->slug;
$post->body = $request->body;
$post->save();
Session::flash('success', 'SUCCESS MESSAGE GOES HERE');
return redirect()->route('posts.show', $post->id);
}
这是我的模板文件:
@if(Session::has('success'))
<div class="alert-box success">
<h2>{{ Session::get('success') }}</h2>
</div>
@endif
我的路线:
Route::group(['middleware' => ['web']], function() {
Route::get('/', 'PagesController@getIndex');
Route::resource('posts','PostController');
});
看到会话文件没有成功。我无法弄明白为什么:
a:4:{s:6:"_token";s:40:"EntXIr9tkqAcKarDZhaNxKb6RfcFdFV9ZtF6W7kU";s:9:"_previous";a:1:{s:3:"url";s:30:"http://localhost:8000/posts/35";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}s:9:"_sf2_meta";a:3:{s:1:"u";i:1459467699;s:1:"c";i:1459467699;s:1:"l";s:1:"0";}}
有人可以帮我弄清问题是什么?
答案 0 :(得分:1)
web
中间件不再需要显式应用于routes.php文件中的路由。根据Laravel 5.2.27中的this change,它现在默默地应用于app/Providers/RouteServiceProvider.php
内的路线
以前,您需要将web
中间件明确应用于以下路由:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
});
现在可以像这样实现上述目标:
Route::get('/', function () {
return view('welcome');
});
答案 1 :(得分:0)
确保路线使用&#34; web&#34;中间件访问会话数据。
<?php
//get the input information
$stringName = $_POST["stringName"];//name
$imageString = $_POST["imageString"];//image in string form
$dataX = base64_decode($imageString);//make the string into the image
file_put_contents($stringName, $dataX);//save the image on the server
//everything thing above this line works within Unity
$fileSize = filesize($stringName);//get the file size
//bunch of headers, comments next to the ones I have an idea about
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: imagepng");//gives content type
header("Content-Length: ".$fileSize);//file size
header("Content-Disposition: attachment; filename=".$stringName);//download file path
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
readfile( $stringName );
?>