Laravel 将路由 url 参数作为 null 传递给控制器
Laravel pass route url parameter as null to controller
简短的问题描述:
这部分怎么写:$provider = ""
在这条路线中:
Route::get('/connect/{provider?}', array('as' => 'hybridauth', 'uses' => 'AuthController@hybridauth'));
长版:
我想将其重写到控制器中:
Route::get('connect/{provider?}', array("as" => "hybridauth", function($provider = "")
{
// check URL segment
if ($action == "auth") {
// process authentication
try {
Hybrid_Endpoint::process();
}
catch (Exception $e) {
// redirect back to http://URL/social/
return Redirect::route('hybridauth');
}
return;
}
try {
// create a HybridAuth object
$socialAuth = new Hybrid_Auth(app_path() . '/config/hybridauth.php');
// authenticate with Google
$provider = $socialAuth->authenticate("google");
// fetch user profile
$userProfile = $provider->getUserProfile();
}
catch(Exception $e) {
// exception codes can be found on HybBridAuth's web site
return $e->getMessage();
}
// access user profile data
echo "Connected with: <b>{$provider->id}</b><br />";
echo "As: <b>{$userProfile->displayName}</b><br />";
echo "<pre>" . print_r( $userProfile, true ) . "</pre><br />";
// logout
$provider->logout();
}));
到目前为止我已经知道了:
Route.php
Route::get('/connect/{provider?}', array('as' => 'hybridauth', 'uses' => 'AuthController@hybridauth'));
控制器:
/**
*
*/
public function hybridauth($provider)
{
if (isset($provider))
{
try
{
Hybrid_Endpoint::process();
}
catch (Exception $e)
{
// redirect back to http://URL/connect/
return Redirect::route('hybridauth')->with('provider', $provider);
}
return;
}
try
{
$socialAuth = new Hybrid_Auth(app_path() . '/config/hybridauth.php');
$haProvider = $socialAuth->authenticate($provider);
$userProfile = $haProvider->getUserProfile();
}
catch(Exception $e)
{
// exception codes can be found on HybBridAuth's web site
return $e->getMessage();
}
return $userProfile;
}
你走对了!要使用可选的路由参数,您可以在控制器中为其指定一个默认值,就像您在函数中所做的那样。
public function hybridauth($provider = null)
那你可以做
if (is_null($provider)) { ... }
简短的问题描述:
这部分怎么写:$provider = ""
在这条路线中:
Route::get('/connect/{provider?}', array('as' => 'hybridauth', 'uses' => 'AuthController@hybridauth'));
长版:
我想将其重写到控制器中:
Route::get('connect/{provider?}', array("as" => "hybridauth", function($provider = "")
{
// check URL segment
if ($action == "auth") {
// process authentication
try {
Hybrid_Endpoint::process();
}
catch (Exception $e) {
// redirect back to http://URL/social/
return Redirect::route('hybridauth');
}
return;
}
try {
// create a HybridAuth object
$socialAuth = new Hybrid_Auth(app_path() . '/config/hybridauth.php');
// authenticate with Google
$provider = $socialAuth->authenticate("google");
// fetch user profile
$userProfile = $provider->getUserProfile();
}
catch(Exception $e) {
// exception codes can be found on HybBridAuth's web site
return $e->getMessage();
}
// access user profile data
echo "Connected with: <b>{$provider->id}</b><br />";
echo "As: <b>{$userProfile->displayName}</b><br />";
echo "<pre>" . print_r( $userProfile, true ) . "</pre><br />";
// logout
$provider->logout();
}));
到目前为止我已经知道了:
Route.php
Route::get('/connect/{provider?}', array('as' => 'hybridauth', 'uses' => 'AuthController@hybridauth'));
控制器:
/**
*
*/
public function hybridauth($provider)
{
if (isset($provider))
{
try
{
Hybrid_Endpoint::process();
}
catch (Exception $e)
{
// redirect back to http://URL/connect/
return Redirect::route('hybridauth')->with('provider', $provider);
}
return;
}
try
{
$socialAuth = new Hybrid_Auth(app_path() . '/config/hybridauth.php');
$haProvider = $socialAuth->authenticate($provider);
$userProfile = $haProvider->getUserProfile();
}
catch(Exception $e)
{
// exception codes can be found on HybBridAuth's web site
return $e->getMessage();
}
return $userProfile;
}
你走对了!要使用可选的路由参数,您可以在控制器中为其指定一个默认值,就像您在函数中所做的那样。
public function hybridauth($provider = null)
那你可以做
if (is_null($provider)) { ... }