我正在尝试实现一些自定义Flash消息,并且我遇到了一些重定向后会话数据被破坏的问题.
以下是我创建Flash消息的方法:
flash('Your topic has been created.');
Run Code Online (Sandbox Code Playgroud)
这是flash()函数的声明:
function flash($message, $title = 'Info', $type = 'info')
{
session()->flash('flash', [
'message' => $message,
'title' => $title,
'type' => $type,
]);
}
Run Code Online (Sandbox Code Playgroud)
以下是我使用SweetAlerts检查会话/显示Flash消息的方法.此代码包含在我在所有Blade模板中扩展的主布局文件的底部.
@if(Session::has('flash'))
<script>
$(function(){
swal({
title: '{{ Session::get("flash.title") }}',
text : '{{ Session::get("flash.message") }}',
type : '{{ Session::get("flash.type") }}',
timer: 1500,
showConfirmButton: false,
})
});
</script>
@endif
Run Code Online (Sandbox Code Playgroud)
如果我flash()在显示视图之前调用该函数,上面的代码将起作用,如下所示:
public function show($slug)
{
flash('It works!');
return view('welcome');
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在重定向到另一个页面之前调用它,它将无法工作,如下所示:
public function show($slug)
{
flash('It does not work'); …Run Code Online (Sandbox Code Playgroud) 我正在使用Socket.io我正在构建的聊天应用程序.我正在尝试设置会话,以便我可以在会话之间保持用户的登录.
这是相关的代码:
index.js(服务器)
var cookieParser = require('cookie-parser')();
var session = require('cookie-session')({ secret: 'secret' });
app.use(cookieParser);
app.use(session);
io.use(function(socket, next) {
var req = socket.handshake;
var res = {};
cookieParser(req, res, function(err) {
if (err) return next(err);
session(req, res, next);
});
});
io.on('connection', function(socket){
socket.on('test 1', function(){
socket.handshake.test = 'banana';
});
socket.on('test 2', function(){
console.log(socket.handshake.test);
});
});
Run Code Online (Sandbox Code Playgroud)
chat.js(客户端)
var socket = io();
socket.emit('test 1');
socket.emit('test 2');
Run Code Online (Sandbox Code Playgroud)
上面的代码工作,它将banana按预期打印到控制台.但是,如果我像这样评论'test 1':
var socket = io();
// socket.emit('test 1');
socket.emit('test …Run Code Online (Sandbox Code Playgroud) 我在我的 中使用了一个auto_prepend_file指令.htaccess,如下所示:
php_value auto_prepend_file "includes/init.php"
Run Code Online (Sandbox Code Playgroud)
这是我的文件夹结构:
/php
/css/main.css
/includes/init.php
/lib/functions.php
index.php
.htaccess
Run Code Online (Sandbox Code Playgroud)
这适用于根目录中的所有文件,例如index.php. 但是,它不适用于不在根目录中的所有文件,例如文件夹functions.php中的lib文件。
我收到以下错误:
Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
Fatal error: Unknown: Failed opening required 'includes/init.php'
(include_path='.;C:\xampp\php\PEAR') in Unknown on line 0
Run Code Online (Sandbox Code Playgroud)
似乎它找不到应该在的文件,这是有道理的。
我试图指向我项目的根目录,但我不知道如何在.htaccess.
我尝试将我的.htaccess线路更改为以下内容:
#Same problem
php_value auto_prepend_file "/includes/init.php"
#Won't work for any page now
php_value auto_prepend_file "/php/includes/init.php"
Run Code Online (Sandbox Code Playgroud)
.htaccess无论我在层次结构中的哪个位置,我都需要放入什么才能使路径始终有效?
我的页面中有所有的数组forms。我想遍历所有表单,并找到其中的按钮。如何在没有任何库的情况下仅在Javascript 中实现这一点?
这是我的 JS 代码:
var forms = document.querySelectorAll('form');
for(var i = 0; i < forms.length; i++;){
var form = forms[i];
form.addEventListener('submit', validateForm);
}
function validateForm(e){
e.preventDefault();
var button = ?; // I want to get the button that is a child of this form
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用它来获取作为表单子项的所有按钮:
var buttons = document.querySelectorAll('form button');
Run Code Online (Sandbox Code Playgroud)
但是我怎么能只得到一个作为这种形式的孩子的按钮呢?
我已经多次看到这个问题被问到,但总是要找到一个已知的父母的孩子,有一个特定的id或类似的东西。我不知道页面中会有多少个表单,我无法控制它们是否具有独特性id或其他任何可以在使用querySelectorAll().
有没有可能实现我想要的?
我正在 jQuery 中寻找纯 JS 替代品:
var $form = $('form');
var …Run Code Online (Sandbox Code Playgroud) 我有我的文章资源控制器,像这样:
public function articles()
{
$articles = Article::OrderBy('id','DESC')->paginate(3);
return view('blog', compact('articles'));
}
Run Code Online (Sandbox Code Playgroud)
我想将两个变量传递给我的视图,如下所示:
$day = day of created article
$month = month of created article
return view('blog', compact('articles','day','month'));
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何从数据库中获取这些数据,我可以像这样得到创建日期:
$article->created_at
Run Code Online (Sandbox Code Playgroud)
我怎样才能将日期和月份传递给我的观点?
php ×3
laravel ×2
session ×2
.htaccess ×1
apache ×1
express ×1
include ×1
javascript ×1
laravel-5.1 ×1
laravel-5.2 ×1
node.js ×1
redirect ×1
socket.io ×1