2014-10-02 132 views
1

所以我试图使用Laravel的自定义过滤器来验证我的用户。我有我的LDAP PHP脚本工作,我已基本上将其插入到我的自定义过滤器。但是,我需要通过此脚本输入用户在登录屏幕上输入的用户名和密码;换句话说,我需要通过我的自定义过滤器从登录表单中输入用户名和密码。具有多个参数的基本Laravel路由过滤

这里是我的代码,以帮助解释我的问题:

routes.php文件

Route::group(array('before' => 'ldapTest'), function() { 
    Route::controller('apps', 'AppController', array(
     //named routes here 
    )); 
}); 

filters.php

Route::filter('ldapTest', function() 
{ 
    $username = //how do I get this? 
    $password = //how do I get this? 

    //LDAP logic goes here; assume $ldapConn and $userDN are properly initialized 
    $userBind = @ldap_bind($ldapConn, $userDN, $password); 

    if($userBind) 
    { 
     Auth::login(//what goes here? I want to access $username later on in applications); 
     return Redirect::to('apps/home'); 
    } 
    else 
    { 
     echo 'Incorrect password'; 
    } 
}); 

从阅读文档我明白了,你可以将参数作为字符串传递给过滤器,如下所示:Route::filter('ldapTest:400', function(),但我不明白我如何使用这个来传递我的用户名和密码,使用我认为是Input :: get()的方法。

我该怎么做?

+0

您可以使用Input :: get('username')'来获取输入字段的值,有名称'用户名'。你为什么不使用它? – totymedli 2014-10-02 22:25:33

回答

0

其实你可以将参数传递到您的自定义过滤器,在这种情况下,你的过滤器应该是这样的:

Route::filter('ldapTest', function($route, $request, $param){ 
    //... 
}); 

在你Closure第三个参数将接受您传入的参数,它是$param,所以你能够通过像这样在你的面前过滤器:

array('before' => 'ldapTest:someString') 

因此,在过滤器中,$param将包含someString但在你的情况下,这将是一个有点不同,我认为,是因为你要接收通过表单提交的用户输入,从而让那些你可以使用这样的事情在你的过滤器的处理程序输入(关闭):

$username = $request->get('username'); // Assumed that username is a form field 
$password = $request->get('password'); 

您也可以使用Input::get('username')而不是$request但如果你想它将与$request实例变量一起工作,我宁愿使用它。

0

我也有类似的需要在我的项目,这个解决了(没有那么多优雅,但工作)解决方法:

Route::filter('multiParamFilter', function($route, $request, $params){ 
    list($p1, $p2) = explode(':', $params); 
    //Now you have $p1 and $p2 initialized with parameters 
    .... 
} 

在routes.php文件,你可以拨打:

Route::get('path', array('before' => 'multiParamFilter:p1:p2' ...... 

注:它要求您不要在参数值中使用':'(或至少另一个符号)

+0

使用list($ p1,$ p2)= explode(':',$ params)可以缩短一点。 – 2015-09-11 19:06:58