我已经阅读了一些关于 Laravel 的 CSRF 的问题,但我仍然没有找到如何在 React 中使用它。我的目标是制作一个 POST 表单,在那里我进行 AJAX 调用。
这是我的render( ).
render() {
return (
<form method="post" action="logpage">
<input type="hidden" name="csrf-token" value="{{{ csrf_token() }}}" />
//I'm sure this doesn't have csrf_token.
<input type="text" name ="word" value={this.state.word || ''}/>
<button onClick={this.submit} className="btn btn-flat btn-brand waves-attach waves-effect" data-dismiss="modal" type="button">Save</button>
</form>
);
}
Run Code Online (Sandbox Code Playgroud)
这是提交功能。
submit(){
fetch('/words', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
body: JSON.stringify({
//parameters
})
}).then((response)=>{
console.log(response);
});
}
Run Code Online (Sandbox Code Playgroud)
我假设问题$('meta[name="csrf-token"]').attr('content')是没有发送,因为没有生成令牌。但是,我不知道如何在 …
我一直在自学如何使用enzyme和测试 React 组件jest,但我对如何测试使用 React hooks 的已安装组件一无所知。
这是该组件的简化版本......
const [records, setRecords] = useState([]);
const onChange = async ({ value }) => {
try {
const request = await searchRecords();
console.log("Before setRecords");
setRecords(request.results);
console.log("After setRecords");
} catch (error) { /* error handling */ }
}
Run Code Online (Sandbox Code Playgroud)
测试代码看起来像...
let wrapped;
describe("Sample", () => {
beforeEach(() => {
jest.clearAllMocks();
wrapped = mount(
<Provider store={createStore(reducers, {})}>
<Sample />
</Provider>
);
api.mockImplementation(() => ({ results: searchResult }));
});
afterEach(() => { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Laravel 5 设置图像上传器。
这是表格。
<form action="/edit/{{$id}}" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="hidden" name="_method" value="PUT">
//There are other tags (including other input) here
<input type="file" name="pic" accept="image/*">
<input type="submit" value="Apply Changes">
</form>
Run Code Online (Sandbox Code Playgroud)
这是将从form上面运行的控制器功能。
public function update($id)
{
$this->model->find($id)->fill($this->request->all())->save();
if ($this->request->hasFile('pic')) {
$file = $this->request->file('pic');
$name = $file->getClientOriginalName();
$file->move('assets/serviceimages/', $name);
}
return redirect('/blahblah');
}
Run Code Online (Sandbox Code Playgroud)
您可能已经注意到,这不会将上传的文件存储在assets/serviceimages/目录下,这正是我一直在尝试的。
基本上我一直在关注本教程,但显然我遗漏了一些东西,并且完全不知道它是什么,即使在我查看了API 文档之后也是如此。
在if控制器函数的声明中,确认了$file和$name符合我的预期。
因此,我怀疑问题出在 …
我一直在自学Redux,想知道在Redux状态下存储JWT令牌有多安全。
例如,这是一个负责设置和重置令牌的减速器。
export default function loginReducer(state = {
token: "",
}, action) {
switch (action.type) {
case "SET_TOKEN":
{
return {
...state,
token: action.data,
}
break;
}
//other cases here
return state
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以按照以下方式存储令牌。
handleSubmit(values) {
//Calling an API to get a token.
}).then((response) => {
response.json().then((jsonReponse) => {
//This is where the token is stored!
this.props.dispatch(loginAction.setToken(jsonReponse.token));
});
});
}
Run Code Online (Sandbox Code Playgroud)
使用Redux的主要目的是在一个地方组织状态,因此我认为在这里维护令牌是合理的。
但是,我还没有找到一个好的信息资源来说明这样做的安全性/脆弱性。
(我发现了几篇有关localStorage vs Cookies的文章。 据我研究,显然Cookie是存储令牌的安全场所)
任何建议将被认真考虑!
我发现这个问题令人困惑(但也很有趣),我想向这里的人寻求见解。
我一直在自学JSP和相关技术。我要做的是从JSP到servlet检索参数,然后将它们与If()结合使用。这是我的编码的一部分。
if ((request.getParameter("ID_A") != null || request.getParameter("Password_A") != null) &&
(request.getParameter("ID_B") != null || request.getParameter("Password_B") != null)) {
errorMessage = "Information is detected for the both side";
request.setAttribute("errorMessage", errorMessage);
request.getRequestDispatcher("4_enter_personal_info.jsp").forward(request, response);
} // Other conditions...
Run Code Online (Sandbox Code Playgroud)
这是JSP的一部分(上一步)
<form action="PersonalInfor_to_confirmation_servlet" method="POST">
<h2>For an existing customer</h2>
<p>Customer ID</p>
<input type="text" name="ID_A" value="" />
<p>Password</p>
<input type="text" name="Password_A" value="" />
<br>
<h2>For a new customer</h2>
<p>Set your customer ID</p>
<input type="text" name="ID_B" value="" />
<p>Set your password</p>
<input type="text" name="Password_B" value="" />
//There …Run Code Online (Sandbox Code Playgroud) 我差不多整整一天都在寻找如何做到这一点.
起初,我认为这可以通过......来完成
Right click the project name -> "Add files" -> choosing a .cpp file which contains the class you need and the corresponding header file.
然后,.cpp文件和头文件与其原始文件夹一起出现.在此之后,我写#include"random.h"了一个需要使用的项目random.h及其功能.
但是,这会产生错误,表明这一点fatal error: random.h: No such file or directory.编译器显然找不到该文件(即使我可以).
我添加了这张照片.

另外,我一直在寻找如何在没有文件夹的情况下添加.cpp和头文件.(例如,在上图中,您将看到random.cpp内部Using_a_class_test包含在一个名为的文件夹中Random.令我遗憾的是,我还没有找到如何消除这样的文件夹.)
如果您提供任何见解,我将不胜感激.
我一直在教自己react-router,现在我想知道应该用哪种方法去另一页.
根据这篇文章(使用react路由器以编程方式导航),您可以转到另一个页面this.props.history.push('/some/path').
然而,老实说,我不太确定window.location.href和之间的区别history.pushState.
据我所知,window.location.href = "/blah/blah";通过新的HTTP调用引导您进入另一个页面,刷新浏览器.
另一方面,什么history.pushState(和this.props.history.push('/some/path'))做推动状态.这显然会改变HTTP引用并因此更新XMLHttpRequest.
使用history.pushState()更改在更改状态后创建的XMLHttpRequest对象的HTTP标头中使用的引用者.
对我来说,听起来两种方法都会进行新的HTTP调用.如果是这样,有什么区别?
任何建议将被认真考虑.
PS
我认为在决定如何转到另一个页面之前,开发人员需要考虑是否有必要从服务器获取数据.
如果您需要从服务器检索数据,那window.location.href就没问题,因为您将进行新的HTTP调用.但是,如果您正在使用<HashRouter>,或者为了速度而想要避免刷新页面,那么什么是一个好方法呢?
这个问题让我发了这篇文章.
我一直在尝试在与Laravel Dusk一起测试时使用内存数据库。
这里有一个文件.env.dusk.local,具有以下值。
DB_CONNECTION=sqlite
DB_DATABASE=:memory:
Run Code Online (Sandbox Code Playgroud)
这是浏览器测试文件的片段。
class ViewOrderTest extends DuskTestCase
{
use DatabaseMigrations;
/** @test */
public function user_can_view_their_order()
{
$order = factory(Order::class)->create();
$this->browse(function (Browser $browser) use ($order) {
$browser->visit('/orders/' . $order->id);
$browser->assertSee('Order ABC'); //Order name
});
}
}
Run Code Online (Sandbox Code Playgroud)
当php artisan dusk被执行时,黄昏开始浏览器测试。
但是,Dusk似乎正在访问我的本地数据库,因为测试浏览器上有一个订单名称,该订单名称仅存在于我的本地数据库中,而“订单ABC”预计会显示在浏览器中。
根据文档,Laravel Dusk允许我们设置环境变量。
要强制Dusk在运行测试时使用其自己的环境文件,请在项目的根目录中创建一个.env.dusk。{environment}文件。例如,如果您要从本地环境启动黄昏命令,则应创建一个.env.dusk.local文件。
我不认为Dusk正在访问单独的数据库。
任何建议将被认真考虑。
摘要:我尝试在 PHP7.2 和 CentOS 7 上使用 Nginx,但找不到 php-fpm.sock
我曾经在Ubuntu 16上使用PHP7.0 。最近,我切换到了CentOS 7。现在我尝试使用 Nginx 和最新的 PHP (即PHP7.2)进行设置
在 Ubuntu 16 上的 PHP7.0 上,php-fpm 的套接字文件存储在...
/run/php/php7.0-fpm.sock
然而,在PHP7.2上。和CentOS7,我看不到/run/php/目录。所以,很明显,PHP7.2和CentOS上的Nginx不能以这种方式配置
我搜索了 Stackoverflow,但大多数帖子都在处理旧版本的 php(比如这个)
如果我运行yum install php-fpm,它会导致"already installed",所以我确信 php-fpm 本身已经存在
如果我运行“ps aux | grep php-fpm”...
我可以看到有一个直接命名的/etc/php-fpm.d,但它只有一个名为 的文件www.conf,而不是套接字文件。
本教程显示 sock/run/php-fpm/www.sock在 PHP7.2 和 CentOS 7 上的存储方式相同,但在我的机器中,只有php-fpm.pid.
那么,sock 文件到哪里去了呢?任何建议将被认真考虑。
PS:我也感兴趣为什么Ubuntu+PHP7和CentOS+PHP7.2在存储PHP文件的位置上有这样的差异
短版
我想要实现的是session用作类的依赖项,最好与 Laravel 的服务提供者一起使用。那可能吗?
长版
最近发现session服务商无法访问,因为...
在 Laravel 中,会话由 StartSession 中间件处理,该中间件在所有服务提供者启动阶段之后执行
(参考)
这对我来说相当不方便。我一直在创建一个电子商务网站,其中购物车正在使用session,并且session应该保留客户已拿起的物品。
这是 constructor 购物车过去的样子......
class Cart
{
private $items;
public function __construct($items)
{
$this->items = $items;
}
//other functions...
}
Run Code Online (Sandbox Code Playgroud)
而且,这里是AppServiceProvider。
class AppServiceProvider extends ServiceProvider
{
//boot
public function register()
{
$this->app->bind(Cart::class, function () {
return new Cart(session('cart'));
});
}
Run Code Online (Sandbox Code Playgroud)
如前所述,session('cart')服务提供商无法访问。
所以,我session('cart')直接把toCart的构造函数。
class Cart
{
private $items;
public function __construct() …Run Code Online (Sandbox Code Playgroud) php ×4
laravel ×3
javascript ×2
laravel-5 ×2
reactjs ×2
c++ ×1
centos ×1
centos7 ×1
class ×1
codeblocks ×1
cookies ×1
enzyme ×1
html5 ×1
interface ×1
jestjs ×1
jsp ×1
laravel-5.2 ×1
laravel-5.5 ×1
laravel-dusk ×1
nginx ×1
react-dom ×1
react-hooks ×1
react-router ×1
redux ×1
servlets ×1
session ×1
window ×1