Php 如果Laravel 4中未给出,则强制URL参数
我有一个网址:Php 如果Laravel 4中未给出,则强制URL参数,php,routing,laravel-4,query-string,Php,Routing,Laravel 4,Query String,我有一个网址:http://localhost/laravel/projectx/public/user/username 此时,它读取用户名,以输出某些结果,比如它是谁的页面,这很好,但如果他们只是键入http://localhost/laravel/projectx/public/user,它会重定向到localhost/user,但我需要它将配置文件用户页面呈现为他们自己的页面(返回http://localhost/laravel/projectx/public/user/meURL)。你
http://localhost/laravel/projectx/public/user/username
此时,它读取用户名
,以输出某些结果,比如它是谁的页面,这很好,但如果他们只是键入http://localhost/laravel/projectx/public/user
,它会重定向到localhost/user
,但我需要它将配置文件用户页面呈现为他们自己的页面(返回http://localhost/laravel/projectx/public/user/me
URL)。你知道我该怎么做吗
我的路线如下,但不起作用
Route::get('/user/', array(
'as' => 'profile',
'uses' => 'ProfileController@user'
));
Route::get('/user/{username}', array(
'as' => 'profile-user',
'uses' => 'ProfileController@user'
));
以及ProfileController中的代码:
public function user($username = null) {
if($username != null) {
$user = User::where('username', '=', $username);
if($user->count()) {
$user = $user->first();
return View::make('profile.user')->with('name', $user->username);
}
}
return View::make('profile')->with('name', Auth::user()->username);
}
public function user($username = null)
{
$user = $username
? User::where('username', '=', $username)->first()
: null;
if ( ! $user && Auth::check())
{
$user = Auth::user();
}
else
{
return Redirect::to('/');
}
return View::make('profile')->with('name', $user);
}
您只能使用一条路线:
Route::get('/user/{username?}', array(
'as' => 'profile-user',
'uses' => 'ProfileController@user'
));
我会在你的控制器里用这样的东西:
public function user($username = null) {
if($username != null) {
$user = User::where('username', '=', $username);
if($user->count()) {
$user = $user->first();
return View::make('profile.user')->with('name', $user->username);
}
}
return View::make('profile')->with('name', Auth::user()->username);
}
public function user($username = null)
{
$user = $username
? User::where('username', '=', $username)->first()
: null;
if ( ! $user && Auth::check())
{
$user = Auth::user();
}
else
{
return Redirect::to('/');
}
return View::make('profile')->with('name', $user);
}
路由::get('user/{username?}',…不起作用,当没有指定用户时,仍然重定向到
localhost/user
。是的,我忘记添加?
。已编辑。