标签: laravel-7

Laravel 7 HTTP 客户端 - 无法使用“body”发送 POST 请求

使用 Laravel 7.6 及其内置的 HTTP 客户端。

body我正在尝试将原始 JSON 格式的简单 POST 请求发送到我的其他域,但没有成功:

$response = Http::post('https://example.com', [
    'body' => '{ test: 1 }'
]);
Run Code Online (Sandbox Code Playgroud)

我收到 400 Bad Request - 客户端错误 - 因为我的服务器期望body这是强制性的。

我在这里缺少什么?

http request laravel guzzle laravel-7

3
推荐指数
1
解决办法
1万
查看次数

Laravel 7:重定向到不同守卫的不同登录

我正在使用身份验证中间件并创建管理防护来控制管理访问。当我尝试访问管理路由时遇到一些问题,我想将与管理路由相关的未经身份验证的流量重定向到 /admin/login 页面,但它没有这样做,而是将我重定向到 /login 页面。我不知道如何获取与身份验证类中的路由关联的守卫。

 protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是代码,我希望是这样的:

 protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            if(Auth::guard('admin'))
               return route('admin.login);
            else
               return route(login);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为我唯一的参数是 $request。

这些是我的路线...


//Admin Routes
Route::middleware(['auth:admin'])->group(function () {
Route::get('/admin', 'AdminController@index')->name('admin');
Route::get('/newDoncente', 'AdminController@addDocenteView')->name('newDocente');

//Docentes Routes
Route::get('/docentes', 'Docente\DocenteController@getDocentesView')->name('getDocentesView');
Route::get('/editDocente/{id}', 'Docente\DocenteController@editDocenteView')->name('editDocentesView');
Route::get('/docentesTables', 'Docente\DocenteController@getDocentesDatatables')->name('getDocentesTables');
Route::get('/docente/{id}', 'Docente\DocenteController@getDocenteView')->name('getDocenteView');
Run Code Online (Sandbox Code Playgroud)

谢谢。

php authentication guard laravel laravel-7

3
推荐指数
1
解决办法
6576
查看次数

Laravel 7 单元测试到注册路由重定向到主页

我正在使用提供的几乎完全开箱即用的注册流程Laravel 7

当我尝试通过网络浏览器中的表单提交无效的注册表单时,我会返回到注册页面,其中包含错误列表,这正是我期望发生的情况。

但是,我正在尝试为此功能编写一个单元测试,但由于某种原因,当我在单元测试中发出相同的请求时,我得到了重定向到 root page 的响应/

为什么我的单元测试没有返回与通过浏览器发出相同请求时相同的 HTML 响应?我怀疑这可能是由于请求中缺少发送的 CSRF 令牌,但根据文档,CSRF 中间件应该在单元测试期间被禁用。

运行测试时,CSRF 中间件会自动禁用。

这是我的单元测试代码:

public function testPostInvalidRegistration(){

    $response = $this->post("/register",[
        'first_name' => $this->faker->name
    ]);
    
    $response->dumpSession();
    $response->dump();
    $response->dumpHeaders();

}
Run Code Online (Sandbox Code Playgroud)

这是输出$response->dump()

public function testPostInvalidRegistration(){

    $response = $this->post("/register",[
        'first_name' => $this->faker->name
    ]);
    
    $response->dumpSession();
    $response->dump();
    $response->dumpHeaders();

}
Run Code Online (Sandbox Code Playgroud)

$response->dumpSession()可以看到验证器已运行并列出了错误。

php phpunit unit-testing laravel-7

3
推荐指数
1
解决办法
1012
查看次数

Laravel:如何向第 3 方伪造 http 请求并模拟 json 响应

我正在努力弄清楚什么可能是非常基本的东西,我会被嘲笑离开这里,但我希望也许它也能对其他人有所帮助。

我正在尝试Http在功能测试中模拟/测试请求。我仍在学习好的/更好/最好的测试技术,所以也许有更好的方法。

// MyFeatureTest.php

$user = factory(User::class)->create(['email' => 'example@email.com']);

// Prevent actual request(s) from being made.
Http::fake();

$this->actingAs($user, 'api')
    ->getJson('api/v1/my/endpoint/123456')
    ->assertStatus(200);

Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我的请求如下所示:

public function myFunction() {
    try {
        $http = Http::withHeaders([
                'Accept' => 'application/json',
                'Access_Token' => 'my-token',
                'Content-Type' => 'application/json',
            ])
            ->get('https://www.example.com/third-party-url, [
                'foo' => 'bar,
        ]);

            
        return new MyResource($http->json());
    } catch (RequestException $exception) {
        Log::error("Exception error: " . print_r($exception, true));
    }
}
Run Code Online (Sandbox Code Playgroud)

我想模拟我收到 200 响应,并且理想情况下模拟来自资源的预期 json。当端点位于我的应用程序本地时(不调用第三方),我已经成功地进行了此测试。这就是我过去所做的:

$http->assertStatus(200)
    ->assertJsonStructure([
        'type', 'id', 'attributes' => [
            'email', 'uuid', …
Run Code Online (Sandbox Code Playgroud)

testing phpunit laravel laravel-7

3
推荐指数
1
解决办法
1万
查看次数

仅允许来自域的请求并阻止其他来源

我想允许某些路由仅响应我的前端网站发出的请求,这意味着出于安全原因阻止其他来源(例如邮递员)并仅允许来自前端域的请求。

是否可以?

例如,我有一个网页来检查链接的动态值并验证链接上的令牌是否在数据库中,我可以考虑添加验证码,这样机器人就无法检查所有可能的组合,但这不是 100%安全的。

laravel laravel-7

3
推荐指数
1
解决办法
7938
查看次数

扩展/重写 Laravel 验证器类

在 Laravel 8.3 中,他们引入了一个新功能stopOnFirstFailure,一旦规则失败,就会完全停止验证。我想在Laravel 7中使用这个功能。在检查Laravel 8的vendor/laravel/framework/src/Validation/Validator.php时,我发现stopOnFirstFailure只是在Validator.phppasss函数中添加了一个if语句,如果受保护的变量stopOnFirstFailure为 true,则中断验证循环。是否可以通过扩展/覆盖 Validator.php 类在 Laravel 7 中实现这些?我一直在研究扩展 Laravel 核心类,并偶然发现了这篇文章,但它有点令人困惑,因为该文章只展示了如何重写特定函数。就我而言,我需要声明一个受保护的变量,覆盖一个函数并声明一个新函数。

Laravel 8 Validator.php 代码:

声明变量:

/**
     * Indicates if the validator should stop on the first rule failure.
     *
     * @var bool
     */
    protected $stopOnFirstFailure = false;
Run Code Online (Sandbox Code Playgroud)

stopOnFirstFailure函数:

  /**
     * Instruct the validator to stop validating after the first rule failure.
     *
     * @param  bool  $stopOnFirstFailure
     * @return $this
     */
    public function …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-validation laravel-7 laravel-8

3
推荐指数
1
解决办法
5096
查看次数

消息:调用未定义的方法 Illuminate\Contracts\Validation\Rule::unique()

我正在尝试验证表单中的字段并使用唯一的规则

 $validator = Validator::make($request->all(),[
    'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->where(function ($q) {
   $q->whereNotNull('password');
}) ],
    'password' => ['required', 'string', 'min:5', 'confirmed','min:8'],
    'password_confirmation' => ['min:8']
]);
Run Code Online (Sandbox Code Playgroud)

收到此错误:

message: "调用未定义的方法 Illuminate\Contracts\Validation\Rule::unique()",

使用了这个命名空间:

使用 Illuminate\Contracts\Validation\Rule;

有什么建议请谢谢

laravel laravel-7

3
推荐指数
1
解决办法
4325
查看次数

iframe 中的 419 页面已过期 Laravel 页面

Laravel-7我正在打开一个在 Angular 项目中使用 iframe 内部开发的页面。页面正在显示记录。但是在创建或更新记录时,页面会抛出 419 错误。

我已经更新了“same_site”=>“none”,但没有帮助。

这两个项目都部署在 GCP 中

我已附上我的 session.php 文件。

<?php

use Illuminate\Support\Str;

return [

/*
|--------------------------------------------------------------------------
| Default Session Driver
|--------------------------------------------------------------------------
|
| This option controls the default session "driver" that will be used on
| requests. By default, we will use the lightweight native driver but
| you may specify any of the other wonderful drivers provided here.
|
| Supported: "file", "cookie", "database", "apc",
|            "memcached", "redis", "dynamodb", "array"
|
*/

'driver' …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-7

3
推荐指数
1
解决办法
4370
查看次数

Laravel 7.x:fresh 和 refresh 方法的区别?

在阅读 Laravel 文档时,我遇到了一个名为freshand的方法,该方法refresh基于 Eloquent 模型。请解释它们之间的主要区别?我很难理解这些。

laravel eloquent eloquent-relationship laravel-7

2
推荐指数
1
解决办法
3641
查看次数

Laravel 7 中的返回响应图像

我正在尝试创建一个迷你random图像生成器,作为我尝试过的开始:

Route::get('/img/UoJb9wl.png','Image@random');
Run Code Online (Sandbox Code Playgroud)

我所做的就是返回这个

return '<img src="/assets/fe/img/welcome.jpeg" alt="Smiley face" width="200">';
Run Code Online (Sandbox Code Playgroud)

然后,我在实时站点上进行了测试:

如果我转到 URL,我会看到图像已加载

https://www.bunlongheng.com/img/UoJb9wl.png
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如果我像这样在像JSFiddle这样的网站上导入

<img src="https://www.bunlongheng.com/img/UoJb9wl.png">
Run Code Online (Sandbox Code Playgroud)

我看不到。

为什么 ?


编辑

尝试 #2

return env('APP_URL').'/assets/fe/img/welcome.jpeg';
Run Code Online (Sandbox Code Playgroud)

我现在返回图像路径

但在我我仍然没有渲染的jsfiddle

<img src="https://www.bunlongheng.com/img/UoJb9wl.png">
Run Code Online (Sandbox Code Playgroud)

php image laravel laravel-5 laravel-7

2
推荐指数
1
解决办法
261
查看次数