2013-09-27 8 views
7

मैं अपने ऐप में सही ढंग से प्रेषित पैकेज स्थापित करने की कोशिश कर रहा हूं।लार्वेल संत्री रीडायरेक्ट :: इरादा नहीं कर रहा

मैं उपयोगकर्ता को लॉग इन और आउट करने और रूट की रक्षा कर सकता हूं लेकिन मुझे ठीक से काम करने के लिए redirect::intended नहीं मिल रहा है। मेरी समझ यह है कि उपयोगकर्ता को उस रूट पर वापस ले जाया जाएगा जिसे मूल रूप से लॉगिन पृष्ठ पर निर्देशित करने से पहले कहा जाता है। फिलहाल यह डिफ़ॉल्ट पृष्ठ पर रीडायरेक्ट करता रहता है। इस समूह मैं सभी सुरक्षित मार्गों रखा है के भीतर

Route::group(array('before' => 'sentryAuth'), function() {...} 

:

मेरी routes.php में मैं निम्नलिखित समूह की स्थापना की है।

मेरी filters.php में मैं निम्नलिखित फिल्टर है:

Route::filter('sentryAuth', function() { 

if (!Sentry::check()) { 

    return Redirect::route('login'); 
} 
}); 

मार्ग :: फिल्टर ('sentryGuest', function() {

if (Sentry::check()) { 
    return Redirect::intended('dashboard'); 
} 
}); 

मेरी userController में मैं निम्नलिखित कोड है :

public function postAuthenticate() 
{ 
    try { 
     // Set login credentials 
     $credentials = array(
      'email' => Input::get('email'), 
      'password' => Input::get('password') 
     ); 

     // Try to authenticate the user 
     $user = Sentry::authenticate($credentials, false); 
    } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) { 
     echo 'Login field is required.'; 
    } 
    catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) { 
     echo 'Password field is required.'; 
    } 
    catch (Cartalyst\Sentry\Users\UserNotFoundException $e) { 
     echo 'User was not found.'; 
    } 
    catch (Cartalyst\Sentry\Users\WrongPasswordException $e) { 
     echo 'Wrong password, try again.'; 
    } 
    catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) { 
     echo 'User is not activated.'; 
    } 

    if (!Sentry::check()) { 
     return Redirect::to('user/login'); 
    } else { 
     return Redirect::intended('dashboard'); 
    } 
} 

मैं में लॉग इन किया जा रहा है बिना कोई पृष्ठ एक्सेस करने की कोशिश की है 'बुकिंग/बनाने' मैं टी लिया मिलता है। ओ लॉगिन पेज, लॉग इन करें लेकिन फिर मुझे डैशबोर्ड पर ले जाता है न कि बुकिंग/बनाने के लिए।

पूर्वाह्न मुझे यहां कुछ याद आ रहा है? क्या काम करने के इरादे से मुझे अतिरिक्त कोड चाहिए ??

उत्तर

8

मुझे इस बारे में निश्चित नहीं है, क्योंकि मैं अपने वर्तमान प्रोजेक्ट के लिए सेंट्री का उपयोग नहीं कर रहा हूं .. इसलिए यह सिर्फ एक झटका है।

ऐसा लगता है .. कि जब से तुम SentryAuth और नहीं laravel 4 में देशी प्रमाणीकरण वर्ग के लिए इस्तेमाल किया, इरादा यूआरएल के लिए सत्र आइटम सेट नहीं है मैं Redirector वर्ग पर एपीआई पर देखा और मैं इस देखा:

public function intended($default, $status = 302, $headers = array(), $secure = null) 
{ 
    $path = $this->session->get('url.intended', $default); 

    $this->session->forget('url.intended'); 

    return $this->to($path, $status, $headers, $secure); 
} 

और जैसा कि कोड कहता है, सत्र कुंजी 'url.intended' है। मैंने देशी एथ फ़िल्टर का उपयोग करके इसे सत्यापित किया है और इच्छित यूआरएल Session::get('url.intended') पर अपेक्षित है ..

इसलिए इसका एक संभावित समाधान मैन्युअल रूप से सत्र में सेट करना है। एक उदाहरण होगा:

अपने फिल्टर

Route::filter('sentryAuth', function() { 

    if (!Sentry::check()) { 
     Session::put('loginRedirect', Request::url()); 
     return Redirect::route('login'); 
    } 
}); 

पर अपने postAuthenticate पर() विधि कोड की

if (!Sentry::check()) { 
    return Redirect::to('user/login'); 
} else { 
    // Get the page we were before 
    $redirect = Session::get('loginRedirect', 'dashboard'); 

    // Unset the page we were before from the session 
    Session::forget('loginRedirect'); 

    return Redirect::to($redirect); 
} 

भागों में संदर्भ के लिए here से ले जाया गया ..^_^

+0

बहुत बढ़िया जवाब:

 Route::filter('auth', function() { if (! Sentry::check()) { return Redirect::guest(route('login')); } }); 

2.After एक उपयोगकर्ता के लॉग इन करें: अपने प्रमाणन फिल्टर 1.in

: आप इच्छित पुनर्निर्देशन लागू करने के लिए दो चीजों की आवश्यकता जैसा सोचा था। धन्यवाद – Ray

4

@ reikyoushin का जवाब उत्कृष्ट है। यहां थोड़ा अलग संस्करण है।

मार्ग।php

// NOTE: do NOT name your filter "auth" as it will not override 
//  Laravel's built-in auth filter and will not get executed 
Route::filter('sentryAuth', function() 
{ 
    // check if logged in or not 
    if (! Sentry::check()) 
    { 
     // the guest() method saves the intended URL in Session 
     return Redirect::guest('user/login'); 
    } else { 
     // now check permissions for the given route 
     $user = Sentry::getUser(); 
     if (! $user->hasAccess(Route::currentRouteName())) 
     { 
      // redirect to 403 page 
      return Response::make('Forbidden', 403); 
     } 
    } 
}); 

// Protected routes 
Route::group(array('before' => 'sentryAuth', function() 
{ 
    Route::get('admin', function() { return View::make('admin.index'); }); 
}); 

और अपनी प्रवेश समारोह में:

return Redirect::route('login'); 

अतिथि के

return Redirect::guest('login'); 

बजाय:

public function login() { 
    try { 
     $creds = array(
      'email' => Input::get('email'), 
      'password' => Input::get('password') 
     ); 
     $user = Sentry::authenticate($creds); 
     return Redirect::intended('dashboard'); 
    } catch (Exception $e) { 
     // handle exceptions 
    } 
} 
10

अपने filters.php में उपयोग सुनिश्चित करें समारोह टी सेट करेगा वह सही ढंग से काम करने के लिए इच्छित() के लिए सत्र चर को सही करता है।

0

सबसे आसान तरीका।

अपने प्रमाणीकरण फिल्टर में निम्नलिखित का उपयोग करें:

Route::filter('sentryAuth', function() 
{ 
    if (! Sentry::check()) 
    {  
     return Redirect::guest(route('login')); 
    } 
}); 

अपने नियंत्रक में:

public function postAuthenticate() 
{ 
    try { 
      $credentials = array(
      'email' => Input::get('email'), 
      'password' => Input::get('password') 
     ); 

     $user = Sentry::authenticate($credentials, false); 
    } catch() { 
     // validation errors 
    } 

    //firstly, Laravel will try to redirect intended page. 
    //if nothing saved in session, it will redirect to home. 
    //you can use any route instead of home. 

    return Redirect::intended(route('home')); 
} 

हो गया। संतरी की

बाहर:

इस कोड को प्रमाणीकरण पुस्तकालय से किसी भी प्रकार के साथ प्रयोग किया जा सकता है। परिवर्तन काम किया है और पुन: निर्देशित होता है -

 
//firstly, Laravel will try to redirect intended page. 
//if nothing saved in session, it will redirect to home. 
//you can use any url instead of '/' 

return Redirect::intended('/'); 
संबंधित मुद्दे