2016-03-12 26 views
2

使用用戶名作爲頁面地址。例如我想直接訪問頁面的用戶名這樣的:我想在URL

mysite.com/username 

重定向到我的SiteController。

回答

2

幾種解決方案,但我認爲有更類似Yii的方式來做到這一點。

首先你需要的缺省.htacces文件在您的Web文件夾:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php 

然後,你需要的urlManager在任您共同/配置/主或您的前端/普通/主。

'components' => [ 
    'urlManager' => [ 
     'class' => 'yii\web\UrlManager', 
     // Disable index.php 
     'showScriptName' => false, 
     // Disable r= routes 
     'enablePrettyUrl' => true, 
     'rules' => [ 
      '<controller:\w+>/<id:\d+>' => '<controller>/view', 
      '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', 
      '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 
      '<username:\w+>' => 'site/profile' // This will handle your usernames 
     ], 
    ], 
], 

最後一個actionProfile($用戶名)在您的網站控制器

public function actionProfile($username) { 
    $model = \common\models\User::find()->where(['username' => $username])->one(); 
    die($model->username); 
} 

您也可能會發現這些鏈接有用:

http://www.yiiframework.com/doc-2.0/yii-behaviors-sluggablebehavior.html http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html

編輯: 等一下!您希望所有網站/操作都能在yoururl.com/actions上運行?

然後你需要

'components' => [ 
     'urlManager' => [ 
     'class' => 'yii\web\UrlManager', 
     // Disable index.php 
     'showScriptName' => false, 
     // Disable r= routes 
     'enablePrettyUrl' => true, 
     'rules' => [ 
      '<controller:\w+>/<id:\d+>' => '<controller>/view', 
      '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', 
      '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 
      '<alias:index|all|your|actions|here>' => 'site/<alias>', 
     ], 
    ], 

或者:

'components' => [ 
     'urlManager' => [ 
     'class' => 'yii\web\UrlManager', 
     // Disable index.php 
     'showScriptName' => false, 
     // Disable r= routes 
     'enablePrettyUrl' => true, 
     'rules' => [ 
      '<controller:\w+>/<id:\d+>' => '<controller>/view', 
      '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', 
      '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 
      '<action:\w+' => 'site/<action>', 
     ], 
    ], 
+0

謝謝,它的工作時,我用site.com/profile/username,但我想有site.com/username – Farshid

+0

Okey,所以原始的網址是/網站/配置文件/用戶名? –

+0

是的,你有想法嗎? – Farshid

2

它被命名爲URL rewriting,由服務器通過某個服務器模塊提供。例如apache中的mod_rewrite

配置完成後,它將從URL中獲取所需參數,並通過QUERY_STRING環境變量以通常方式將它們發送到腳本。

你不應該改變你的php代碼。

2

試試這個在.htaccess

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]*)$ /SiteControllet.php?id=$1 [L] 

這將實際工作爲
Sitecontroller.php?用戶名= somename 你需要使用user.php的查找數據。

當然首先你需要在apache配置中啓用 mod_rewrite 。

Umcomment這條線在配置文件:

LoadModule rewrite_module modules/mod_rewrite.s 

記住和.htaccess應在根文件夾。

0

創建一個文件名爲」的.htaccess'和寫入文件

RewriteEngine On 
RewriteCond %{filename} !-d 
RewriteCond %{filename} !-f 
RewriteRule ^([^/]*)$ /user.php?id=$1 [L] 

希望它有助於在下面! :D 或更高級的選項,你可以搜索「網址重寫」。

P.S.我不是英語母語的人。

+0

我用htaccess的代碼prettyurl – Farshid

+1

問計upvoting是瘸子。請注意這一點。 –