通过HTTP请求实例使用会话与使用Laravel中的全局会话帮助程序之间的区别 [英] difference between using the session via an HTTP request instance versus using the global session helper in Laravel

查看:71
本文介绍了通过HTTP请求实例使用会话与使用Laravel中的全局会话帮助程序之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到与两种会话访问方法有关的任何信息. Laravel 5.3中来自HTTP请求实例的$request->session()和来自会话帮助器的session(). 有什么区别或何时使用?

使用P.H.P单元时如何向以下控制器方法发送获取请求

public function testMyMethod(Request $request){
$userExist = $request->session()->exists('user_id');
}

解决方案

服务容器是Laravel架构的核心.所有服务,组件和依赖项都在此处注册,您可以在需要时要求它们中的任何一个.

但是Laravel提供了多种方式来询问".您具有全局帮助程序功能,外观,方法签名中的组件实例的正确",纯净"注入.我认为Laravel哲学的很大一部分是干净,简单,直观的API.在这种情况下,您可以根据自己的喜好来定义什么是简洁",并且根据定义,任何与您的样式匹配的内容对您来说都是直观的.

我知道在PHP社区中,关于哪种方法是最佳"的争论一直很激烈,外观一直存在争议,传统的依赖注入OOP纯粹主义者说,唯一正确的方法可能是使用控制器方法签名注入对象.

最后,各种方法中的任何一种都只是从同一服务容器包中抓取对象.在性能方面(我敢打赌两个间接函数调用不会影响您的性能)或其他方面没有任何区别.因此,请更好地使用适合自己风格的东西.如果我在典型的控制器中,我个人还是会进行适当的"注入,但是如果全局帮助器使上下文中的代码更清晰,可读性更好,也许我会使用全局帮助器.

<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\Session\Session;
use Facades\Illuminate\Contracts\Session\Session as SessionRealTimeFacade;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request as RequestFacade;
use Illuminate\Support\Facades\Session as SessionFacade;
use PHPUnit\Framework\Assert;

class TestController extends Controller
{
    public function sessionTest(Request $request, Session $session)
    {
        $options = [
            $request->session()->get('_token'),
            session()->get('_token'),
            session('_token'),
            $session->get('_token'),
            SessionFacade::get('_token'),
            SessionRealTimeFacade::get('_token'),
            app('session')->get('_token'),
        ];

        array_reduce(
            $options,
            function ($one, $other) {
                Assert::assertEquals($one, $other);
                return $other;
            },
            array_shift($options)
        );

        return 'All tests passed!';
    }    
}

顺便说一句,正如您所看到的,您不仅有两个选择:)

当然,这不仅适用于会话,获取请求数据,获取数据库连接等等还有同样的道理.

(而且,我认为您还没有5.3中的实时外观)

I could not find any information related two types of session access methods. $request->session() from HTTP request instance and session() from session helper in Laravel 5.3 . Is there any difference or which one to use when ?

How to send a get request to below controller method when using P.H.P unit

public function testMyMethod(Request $request){
$userExist = $request->session()->exists('user_id');
}

解决方案

The Service Container is the core of the Laravel architecture. All services, components, and dependencies are registered there, and you can ask for any of them whenever you need it.

But Laravel provides more than one way to "ask for it". You have the global helper functions, you have facades, you have the "proper", "purer" injection of the component instance in the method signature. I think a big part of the philosophy of Laravel is a clean, easy, intuitive API. And in this case, it can be left up to your personal preference to define what "clean and easy" is, and whatever matches your style will be, by definition, intuitive to you.

I know there have been heated debates in the PHP community as to which method is "best", facades have been controversial, traditional dependency injection OOP purists say the only right way may be injecting the object with the controller method signature...

In the end, any of this various methods just grab the object from the same service container bag. It makes no difference performance-wise (I bet two function calls of indirection won't hit your performance) or otherwise. So, use whatever suits your style better. I personally do the "proper" injection anyway if I'm in a typical controller, but maybe I use the global helper if it makes for a cleaner, more readable code in its context.

<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\Session\Session;
use Facades\Illuminate\Contracts\Session\Session as SessionRealTimeFacade;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request as RequestFacade;
use Illuminate\Support\Facades\Session as SessionFacade;
use PHPUnit\Framework\Assert;

class TestController extends Controller
{
    public function sessionTest(Request $request, Session $session)
    {
        $options = [
            $request->session()->get('_token'),
            session()->get('_token'),
            session('_token'),
            $session->get('_token'),
            SessionFacade::get('_token'),
            SessionRealTimeFacade::get('_token'),
            app('session')->get('_token'),
        ];

        array_reduce(
            $options,
            function ($one, $other) {
                Assert::assertEquals($one, $other);
                return $other;
            },
            array_shift($options)
        );

        return 'All tests passed!';
    }    
}

And by the way, as you can see you have more than just 2 options :)

Of course, this doesn't apply to sessions only, the same goes for getting the request data, getting DB connection, and so much more.

(Also, I think you don't have the real-time facades in 5.3 yet)

这篇关于通过HTTP请求实例使用会话与使用Laravel中的全局会话帮助程序之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆