GVKun编程网logo

Laravel PHP-如何在保存到数据库之前解码'?/ \ +-'并在之后对其进行编码?

2

如果您对LaravelPHP-如何在保存到数据库之前解码'?/\+-'并在之后对其进行编码?感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解LaravelPHP-如何在保存到数据库之前解码'?/

如果您对Laravel PHP-如何在保存到数据库之前解码'?/ \ +-'并在之后对其进行编码?感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Laravel PHP-如何在保存到数据库之前解码'?/ \ +-'并在之后对其进行编码?的各种细节,此外还有关于$request->only() 在 laravel 7.9 中返回空数组 来自 Laravel 5.4 的文档 Reference来自 Laravel 7.9 的文档 Reference、file_get_contents('php://input') 不适用于 Laravel+Octane / Swoole PHP 代码使用 PHP-FPM使用 Octane+Swoole、git 拖下 laravel 代码后报错 Warning: require (D:\WWW\laravel\bootstrap/../vendor/autoload.php、Laravel 5.2 使用 JWT 完成多用户认证 | Laravel China 社区 - 高品质的 Laravel 开发者社区 - Powered by PHPHub的实用技巧。

本文目录一览:

Laravel PHP-如何在保存到数据库之前解码'?/ \ +-'并在之后对其进行编码?

Laravel PHP-如何在保存到数据库之前解码'?/ \ +-'并在之后对其进行编码?

如何解决Laravel PHP-如何在保存到数据库之前解码''?/ \\ +-''并在之后对其进行编码?

问题 用户总是在产品名称中添加此章程(''\\ /?+-“),并在此之后中断其他功能,例如在产品url中添加SEO

是否有任何PHP或Laravel函数? 还是很容易使其成为模型?

谢谢!

解决方法

使用Str::slug函数创建一个条。最好将其也存储在数据库中以及另一个字段中以进行比较。然后,您可以将Slug用作URL,并按原样保留产品名称,以便在页面上使用。

$slug = \\Str::slug("Bob''s product has a \\ / ? + - characters"); 

// Results in "bobs-product-has-a-characters"

$request->only() 在 laravel 7.9 中返回空数组 来自 Laravel 5.4 的文档 Reference来自 Laravel 7.9 的文档 Reference

$request->only() 在 laravel 7.9 中返回空数组 来自 Laravel 5.4 的文档 Reference来自 Laravel 7.9 的文档 Reference

如何解决$request->only() 在 laravel 7.9 中返回空数组 来自 Laravel 5.4 的文档 Reference来自 Laravel 7.9 的文档 Reference

我在 laravel 5.4 到 7.9 的时候一团糟,我已经阅读了以下文档:https://laravel.com/docs/7.x/requests,但我不清楚:

enter image description here

在 laravel 5.4 中,它返回一个数组:

laravel 5.4

这是 laravel 7.9 中的空数组:

enter image description here

谢谢!

解决方法

您所描述的问题是预期行为,并且在不同版本之间发生了变化。

来自 Laravel 5.4 的文档 Reference

only 方法返回您请求的所有键/值对,即使传入请求中不存在该键。当请求中不存在密钥时,该值将为 null

来自 Laravel 7.9 的文档 Reference

only 方法返回您请求的所有键/值对;但是,它不会返回请求中不存在的键/值对。

file_get_contents('php://input') 不适用于 Laravel+Octane / Swoole PHP 代码使用 PHP-FPM使用 Octane+Swoole

file_get_contents('php://input') 不适用于 Laravel+Octane / Swoole PHP 代码使用 PHP-FPM使用 Octane+Swoole

如何解决file_get_contents(''php://input'') 不适用于 Laravel+Octane / Swoole PHP 代码使用 PHP-FPM使用 Octane+Swoole?

我正在从 Laravel 8 迁移到 Laravel 8 + Octane / Swoole。一切正常,但 PHP://input 始终为空。另外,我检查 $_POST 和 $_SERVER 值。

file_get_contents(''PHP://input'') 被 AWS SNS Message Validator 使用。

阅读 PHP://input 的任何替代方案?

PHP 代码

echo "PHP://input: ".file_get_contents(''PHP://input'');

使用 PHP-FPM

$ curl -i -X POST --data "dataaaa" https://example.com/aws/sns/webhook
PHP://input: dataaaa

使用 Octane+Swoole

$ curl -i -X POST --data "dataaaa" https://example.com/aws/sns/webhook
PHP://input:

解决方法

问题

[+] Building 10.5s (9/9) FINISHED => [internal] load build definition from Dockerfile 0.2s => => transferring dockerfile: 133B 0.0s => [internal] load .dockerignore 0.2s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/python:3.9 0.9s => [auth] library/python:pull token for registry-1.docker.io 0.0s => CACHED [1/3] FROM docker.io/library/python:3.9@sha256:acb4e43d0c66c168e72ceaba5913cde472e4a17017cec9346969c9725a1fea94 0.0s => [internal] load build context 0.1s => => transferring context: 12.01kB 0.0s => [2/3] ADD main.py . 0.5s => [3/3] RUN pip install pillow 6.9s => exporting to image 1.5s => => exporting layers 1.2s => => writing image sha256:95e56740a3427899906e9c6522198f5d749fd13870450b046a2bd874b3a04548 0.0s => => naming to docker.io/library/background-revolution 在 Swoole 上不可用。始终是同一个运行进程。

解决方案:PSR-7 请求

php://input

当然,对于辛烷值,Laravel PSR-7 requests 需要 use Psr\Http\Message\RequestInterface; public function sesSubscriptionWebhook(RequestInterface $request) { // $input = file_get_contents(''php://input''); // dont work on swoole $input = $request->getBody(); } symfony/psr-http-message-bridge

此外,如果您的问题与 AWS SES 相关,您需要将 nyholm/psr7 更改为 Message::fromRawPostData()

git 拖下 laravel 代码后报错 Warning: require (D:\WWW\laravel\bootstrap/../vendor/autoload.php

git 拖下 laravel 代码后报错 Warning: require (D:\WWW\laravel\bootstrap/../vendor/autoload.php

composer    install  执行

 

  Problem 1
    - Installation request for doctrine/annotations v1.5.0 -> satisfiable by doctrine/annotations[v1.5.0].
    - doctrine/annotations v1.5.0 requires php ^7.1 -> your PHP version (5.5.38) does not satisfy that requirement.

报错了,接着

删除 composer.lock 文件,重新执行 composer install,这样就能重新生成 composer.lock 文件了。

 

然而

 


  Problem 1
    - laravelista/ekko 1.4.0 requires php >=5.6.0 -> your PHP version (5.5.38) does not satisfy that requirement.
    - laravelista/ekko 1.3.3 requires php >=5.6.0 -> your PHP version (5.5.38) does not satisfy that requirement.

 

php 版本不匹配,接着

composer install --ignore-platform-reqs     忽略版本匹配

 

oading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 113 installs, 0 updates, 0 removals
  - Installing psr/http-message (1.0.1): Downloading (100%)
  - Installing guzzlehttp/psr7 (1.4.2): Downloading (100%)
  - Installing guzzlehttp/promises (v1.3.1): Downloading (100%)
  - Installing guzzlehttp/guzzle (6.3.0): Downloading (100%)
  - Installing league/oauth1-client (1.7.0): Downloading (100%)
  - Installing symfony/event-dispatcher (v3.3.13): Downloading (100%)
  - Installing guzzle/guzzle (v3.8.1): Downloading (100%)
  - Installing symfony/polyfill-mbstring (v1.6.0): Downloading (100%)
  - Installing vlucas/phpdotenv (v2.4.0): Downloading (100%)

 

重装完成
---------------------
作者:步石遮刃
来源:CSDN
原文:https://blog.csdn.net/qq_38363371/article/details/78561298
版权声明:本文为博主原创文章,转载请附上博文链接!

Laravel 5.2 使用 JWT 完成多用户认证 | Laravel China 社区 - 高品质的 Laravel 开发者社区 - Powered by PHPHub

Laravel 5.2 使用 JWT 完成多用户认证 | Laravel China 社区 - 高品质的 Laravel 开发者社区 - Powered by PHPHub

Json Web Token#


JWT代表Json Web Token.JWT能有效地进行身份验证并连接前后端。

  • 降地耦合性,取代session,进一步实现前后端分离

  • 减少服务器的压力

  • 可以很简单的实现单点登录

我在实现这个功能的时候查到了这个扩展“tymon/jwt-auth”,最新稳定版是0.5.9。OK照着wiki撸起来,第一步我们先实现API

安装扩展#
composer require tymon/jwt-auth

之后打开config/app.php文件添加service provider 和 aliase

config/app.php#
''providers'' => [
    ....
    Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class,  // 注意这里的名字,下文会提到],''aliases'' => [
    ....
    ''JWTAuth'' => Tymon\JWTAuth\Facades\JWTAuth::class],

OK,现在来发布JWT的配置文件,比如令牌到期时间配置等

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"

最后一步需要生成JWT Key

php artisan jwt:generate
创建API路由#

我在创建Api路由的时候会用到一个“cors”中间件,虽然它不是强制性的,但是后面你会发现报类似这样的错

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://xxx.com/api/register. (Reason: CORS header ''Access-Control-Allow-Origin'' missing)

大致翻译下,“跨源请求阻塞:同源策略不允许读取http://kylesean.com/api/register远程资源。(原因:CORS 头“Access-Control-Allow-Origin” 没有)。” 这就是跨域请求导致的错误消息,当然你可以自定义Header,Origin, Method来解决跨域问题,不过我这边推荐一个package:barryvdh/laravel-cors(最新稳定版是0.8.2),这里安装过程省略。

创建中间件#
php artisan make:middleware CORS

进入app/Http/Middleware,编辑CORS.php

app/Http/Middleware/CORS.php#
namespace App\Http\Middleware;use Closure;class CORS{
    public function handle($request, Closure $next)
    {
        header(''Access-Control-Allow-Origin: *'');

        $headers = [
            ''Access-Control-Allow-Methods''=> ''POST, GET, OPTIONS, PUT, DELETE'',
            ''Access-Control-Allow-Headers''=> ''Content-Type, X-Auth-Token, Origin''
        ];
        if($request->getMethod() == "OPTIONS") {
            return Response::make(''OK'', 200, $headers);
        }

        $response = $next($request);
        foreach($headers as $key => $value)
            $response->header($key, $value);
        return $response;
    }}

Ok,在app/Http/Kernel.php注册中间件

app/Http/Kernel.php#
namespace App\Http;use Illuminate\Foundation\Http\Kernel as HttpKernel;class Kernel extends HttpKernel{
    ...
    ...
    protected $routeMiddleware = [
        ...
        ''cors'' => \App\Http\Middleware\CORS::class,
    ];}

有了这个中间件我们就解决了跨域问题。接下来回到路由

app/Http/routes.php#
Route::group([''middleware'' => [''api'',''cors''],''prefix'' => ''api''], function () {
    Route::post(''register'', ''ApiController@register'');     // 注册
    Route::post(''login'', ''ApiController@login'');           // 登陆
    Route::group([''middleware'' => ''jwt.auth''], function () {
        Route::post(''get_user_details'', ''APIController@get_user_details'');  // 获取用户详情
    });});
建议:过滤掉路由api/*下的csrf_token,方便测试开发#

上面的jwt-auth中间件现在还是无效的,接着创建这个middleware

php artisan make:middleware authJWT

同样的我们需要编辑下这个authJWT.php

app/Http/Middleware/authJWT.php#
namespace App\Http\Middleware;use Closure;use Tymon\JWTAuth\Facades\JWTAuth;use Exception;class authJWT{
    public function handle($request, Closure $next)
    {
        try {
            // 如果用户登陆后的所有请求没有jwt的token抛出异常
            $user = JWTAuth::toUser($request->input(''token'')); 
        } catch (Exception $e) {
            if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException){
                return response()->json([''error''=>''Token 无效'']);
            }else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException){
                return response()->json([''error''=>''Token 已过期'']);
            }else{
                return response()->json([''error''=>''出错了'']);
            }
        }
        return $next($request);
    }}

OK,接着注册该中间件

app/Http/Kernel.php#
namespace App\Http;use Illuminate\Foundation\Http\Kernel as HttpKernel;class Kernel extends HttpKernel{
    ...
    ...
    protected $routeMiddleware = [
        ...
        ''jwt.auth'' => \App\Http\Middleware\authJWT::class,
    ];}

然后,我们创建控制器管理所有的请求

app/Http/Controllers/ApiController.php#
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\User;use Illuminate\Support\Facades\Hash;use Tymon\JWTAuth\Facades\JWTAuth;class ApiController extends Controller{
    /*注册*/
    public function register(Request $request)
    {
        $input = $request->all();
        $input[''password''] = Hash::make($input[''password'']);
        User::create($input);
        return response()->json([''result''=>true]);
    }

    /*登陆*/
    public function login(Request $request)
    {
        $input = $request->all();
        if (!$token = JWTAuth::attempt($input)) {
            return response()->json([''result'' => ''邮箱或密码错误.'']);
        }
        return response()->json([''result'' => $token]);
    }

    /*获取用户信息*/
    public function get_user_details(Request $request)
    {
        $input = $request->all();
        $user = JWTAuth::toUser($input[''token'']);
        return response()->json([''result'' => $user]);
    }}

最后一步我们就来模拟一个请求来测试这个api,为了模拟本地跨域请求,我们简单的新建一个静态页面test.html

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script></head><body></body><script>
    $.ajax({
    url: "http://localhost/api/login",
    dataType: "json",
    type: "POST",
    data: {"email":kylesean@qq.com","password":"123456"},
    success: function (data) {
        alert(data.result)
    }
    // 这里我们用ajax请求测试,当然你也可以用Angular.js  Vue.js});</script></html>

这里我们要注意一下,以上测试我们仍是基于User table的,我们来模拟一下login过程,如果账号密码匹配成功,不出意外将会出现类似:

{
  "result": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL2xvY2FsaG9zdFwvYXBpXC9sb2dpbiIsImlhdCI6MTQ3MzQ1MjUyNSwiZXhwIjoxNDczNDU2MTI1LCJuYmYiOjE0NzM0NTI1MjUsImp0aSI6IjA1M2IzNjliYzYyZjJiZjJmMGMxNjFiNzIxNzY4Y2MzIn0.4WeezpSgEKjNmDFxv1nMU9HxqJgBE7bPyaJDRK4iLeA"}

至此,我们已经实现了jwt的认证功能,那么我们接着完成下一半工作,实现jwt的多用户认证,即Jwt for Multi Auth.
如果你的业务场景是的确需要多用户认证,比如为管理员admin单独生成一张表,恰好字段也是laravel auth user里面默认的name email password remember_token等,那么实现起来就方便的多,官方文档和网上的demo示例已经很多了,但是若结合这个laravel/jwt-auth扩展进行多用户认证,其实坑还是蛮多的,由于该扩展0.5.9似乎不支持多用户认证(反正不会帮我们自定义好guard,当然我们可以自己在AuthServiceProvider里用boot方法实现) 我在其github issue里面看到好多人踩过此坑,结合我遇到的
总结一下,里面一个哥们说,得用^0.1@dev版本(什么鬼,what''s the fuck!),so 继续撸之:

composer.json里修改为#
"require": {
    ...
    "tymon/jwt-auth": "^1.0@dev",  // 修改之前的,Or making a fresh start 
    ...}
同样app.php里进行配置#
''providers'' => [
    ....
    Tymon\JWTAuth\Providers\LaravelServiceProvider::class,  // 上文已经提到过,这里的provider已经不是JWTauthServiceProvider],''aliases'' => [
    ....
    ''JWTAuth'' => Tymon\JWTAuth\Facades\JWTAuth::class],
发布配置文件#
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
生成密钥#

php artisan jwt:secret   // 发现没生成key的方法也变了,不是 php artisan jwt:generate了在.env文件中配置JWT_SECRET = 生成的的secret

接下来就是重点了,要设置好config/auth.php里面的配置项了,这里不能乱设置:

config/auth.php#
/**
 * 默认使用web这个guard
 */''defaults'' => [
        ''guard'' => ''web'',
        ''passwords'' => ''users'',
    ],''guards'' => [
        ''web'' => [
            ''driver'' => ''session'',
            ''provider'' => ''users'',
        ],

        ''api'' => [
            ''driver'' => ''token'',
            ''provider'' => ''users'',
        ],
        /**
         * 这里是我自定义的guard,这里我叫staffs,你也可以根据自己的业务需求设置admins等,并且我
         * 需要实现json web token认证
         */
        ''staffs'' => [
            ''driver''   => ''jwt'',   // 结合扩展这里定义即生效
            ''provider'' => ''staffs''
        ]

    ],''providers'' => [
    ''users'' => [
        ''driver'' => ''eloquent'',
        ''model'' => App\User::class,   // 这里注意修改命名空间 通常是''model'' => App\Models\User::class,
    ],

    /**     * 同样的这里定义自己的provider     */    ''staffs'' => [        ''driver'' => ''eloquent'',        ''model'' => App\Models\Staff::class,    ]    // ''users'' => [    //     ''driver'' => ''database'',    //     ''table'' => ''users'',    // ],],''passwords'' => [    ''users'' => [        ''provider'' => ''users'',        ''email'' => ''auth.emails.password'',        ''table'' => ''password_resets'',        ''expire'' => 60,    ],    /**     * 这里我并没有设置如下,因为我的staff表并没有email字段,默认的重置密码功能暂时没考虑     */   <!-- ''staffs'' => [        ''provider'' => ''staffs'',        ''email'' => ''auth.emails.password'',        ''table'' => ''password_resets'',        ''expire'' => 60,                          ],-->]

下一步,创建我们的staff model

Models\staff.php#
<?phpnamespace App\Models;use Illuminate\Auth\Authenticatable;use Illuminate\Database\Eloquent\Model;use Illuminate\Auth\Passwords\CanResetPassword;use Illuminate\Foundation\Auth\Access\Authorizable;use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;use Tymon\JWTAuth\Contracts\JWTSubject as AuthenticatableUserContract;  class Staff extends Model implements AuthenticatableContract, AuthorizableContract,  AuthenticatableUserContract{
    use Authenticatable, Authorizable, CanResetPassword;

    protected $table = ''staffs'';
    protected $fillable = [''name'', ''phone'', ''password''];
    protected $hidden = [''password'', ''remember_token''];

    public function getJWTIdentifier()
    {
        return $this->getKey(); // Eloquent model method
    }

    /**
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }}

好吧,接下来我们又要添加相关路由了

Route::post(''/api/login'', ''StaffAuthController@login'');Route::post(''/api/register'', ''StaffAuthController@register'');

控制器书写我们的业务逻辑

namespace App\Http\Controllers\Staff;use App\Models\Staff;use Illuminate\Http\Request;use App\Http\Controllers\Controller;use App\Http\Requests;use Illuminate\Foundation\Auth\ThrottlesLogins;use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;use Illuminate\Support\Facades\Validator;use Tymon\JWTAuth\Facades\JWTAuth;use Illuminate\Support\Facades\Auth;class StaffAuthController extends Controller{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    protected $guard = ''staffs'';

    /*注册*/
    public function register(Request $request)
    {
        $this->validate($request, [
            ''phone'' => ''required|max:16'',
            ''password'' => ''required|min:6'',
        ]);
        $credentials = [
            ''phone'' => $request->input(''phone''),
            ''password'' => bcrypt($request->input(''password'')),
        ];

        $id = Staff::create($credentials);
        if ($id) {
            $token = Auth::guard($this->getGuard())->attempt($credentials); // 也可以直接guard(''staffs'')
            return response()->json([''result'' => $token]);
        }
    }

    /*登录*/
    public function login(Request $request)
    {

        $credentials = $request->only(''phone'',''password'');
        if ( $token = Auth::guard($this->getGuard())->attempt($credentials) ) {

            return response()->json([''result'' => $token]);
        } else {
            return response()->json([''result''=>false]);
        }
    }{

到现在,一个基于JWT的多用于认证系统雏形就建立就来了,这里面需要完善的东西很多,比如刷新token,退出登录,增加额外的中间件等,可以参考该扩展issue(Feature: Laravel 5.2 Custom Authentication Guard and Driver。markdown用的不多,排版不好请见谅,如有错误请指正,一起学习,谢谢。

我们今天的关于Laravel PHP-如何在保存到数据库之前解码'?/ \ +-'并在之后对其进行编码?的分享已经告一段落,感谢您的关注,如果您想了解更多关于$request->only() 在 laravel 7.9 中返回空数组 来自 Laravel 5.4 的文档 Reference来自 Laravel 7.9 的文档 Reference、file_get_contents('php://input') 不适用于 Laravel+Octane / Swoole PHP 代码使用 PHP-FPM使用 Octane+Swoole、git 拖下 laravel 代码后报错 Warning: require (D:\WWW\laravel\bootstrap/../vendor/autoload.php、Laravel 5.2 使用 JWT 完成多用户认证 | Laravel China 社区 - 高品质的 Laravel 开发者社区 - Powered by PHPHub的相关信息,请在本站查询。

本文标签: