ds342222 2014-10-02 20:34
浏览 227

具有多个参数的基本Laravel路由过滤

So I'm attempting to authenticate my user's using Laravel's custom filters. I have my LDAP PHP script working and I have essentially plugged it in to my custom filter. However, I need to pass this script the username and password that the user enters on the log in screen; in other words, I need to pass my custom filter this username and password from the log in form.

Here is my code to help explain my problem:

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';
    }
});

From reading the documentation I understand you can pass parameters as strings to filters like so: Route::filter('ldapTest:400', function(), but I don't understand how I could use this to pass my username and password using what I assume would be Input::get().

How can I do this?

  • 写回答

2条回答 默认 最新

  • dongwei3336 2014-10-02 22:27
    关注

    Actually you can pass parameters into your custom filter and in this case your filter should look like this:

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

    In your Closure the third argument will receive the parameter you passed in and it's $param, so you are able to pass it like this in your before filter:

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

    So, in the filter, the $param will contain someString but in your case that would be a bit different I think, because you want to receive the user inputs submitted through a form so to get those inputs you may use something like this in your filter's handler (Closure):

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

    Also you may use Input::get('username') instead of $request if you want but it'll be working with $request instance variable and I would prefer that to use.

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,