在Laravel 5中,如何实现App :: before()过滤器? [英] In Laravel 5 how do I implement an App::before() filter?

查看:279
本文介绍了在Laravel 5中,如何实现App :: before()过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 4.2中,我在过滤器之前安装了该过滤器,该过滤器基于URL(admin.example.com与example.com)设置了Eloquent模型和表

In Laravel 4.2 I had this before filter that set the Eloquent model and table based on the URL (admin.example.com vs example.com)

这是我的过滤器代码:

App::before(function($request)
{       
  // Check if we are using the admin URL
  $host = $request->getHost();
  $parts = explode('.', $host);
  if ($parts[0] == 'admin')
  {
    // Set the config for user info (not sponsor)
    Config::set('auth.model', 'Admin');
    Config::set('auth.table', 'admins');
  }
});

我尝试在laravel 5中为此创建中间件,并具有以下代码:

I tried creating middleware for this in laravel 5 and have this code:

class AdminOrSponsor implements Middleware {

  public function handle($request, Closure $next)
  {   
    $host = $request->getHost();
    $parts = explode('.', $host);
    if ($parts[0] == 'admin'){
        Config::set('auth.model', 'Admin');
        Config::set('auth.table', 'admins');
    }

    return $next($request);
  }

}

在我的routes.php文件中,我将基于auth.model设置来设置要调用的控制器,如下所示:

In my routes.php file I am setting the controller that is called based on the auth.model setting like this:

Route::get('/auth/login', Config::get('auth.model') . 'Controller@getLogin');
Route::post('/auth/login', Config::get('auth.model') . 'Controller@postLogin');
Route::get('/auth/logout', Config::get('auth.model') . 'Controller@getLogout');

我发现路由都是在中间件之前读取的,因此我尝试通过Config :: set()进行的更改没有发生.我只得到auth.php配置文件中设置的值.

What I found is that the routes are all read prior to the middleware so the change I am trying to make through Config::set() isn't happening. I am only getting the value that is set in the auth.php config file.

我在做什么错?在Laravel 5中该怎么做?

What am I doing wrong and how should I do this in Laravel 5?

推荐答案

似乎您想根据客户端的主机名加载不同的路由.

It seems you want to load different routes depending on the client's hostname.

我了解您的解决方案,但这是一个小技巧,如果您想对其进行单元测试,就会遇到麻烦.配置文件是在路由之前加载的,因此除非您依赖$ _SERVER(这也会破坏单元测试),否则无法根据请求设置路由.

I understand your solution, but it is sort of a hack and you'll run into trouble when you want to unit test this, if you can even get it to work. The config is loaded before the routes, so setting routes based on the request is impossible, unless you rely on $_SERVER (which also breaks unittests).

我将执行以下操作:

  1. 像这样创建路由:

  1. Create the routes as such:

Route::get('/auth/login', 'AuthController@getLogin');
Route::get('/auth/login/admin', 'AdminsController@getLogin');
Route::get('/auth/login/sponsors', 'SponsorsController@getLogin');

  • 创建一个中间件,以防止发起者访问AdminsController,反之亦然.

  • Create a middleware to prevent access to the AdminsController by the sponsors, and vice-versa.

    在AuthController中,根据主机执行从auth/login到管理员或赞助者的重定向.

    Inside the AuthController, perform a redirect from auth/login to either admins or sponsors, depending on the host.

    然后,您仅使用标准" laravel功能,并且可以确定它不会引起任何奇怪的副作用.

    Then you are only using "standard" laravel features, and you can be sure that it doesn't cause any strange side-effects.

    这篇关于在Laravel 5中,如何实现App :: before()过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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