Enable clean URL in Yii2

后端 未结 11 1136
攒了一身酷
攒了一身酷 2020-11-27 12:36

How can I enable clean urls in Yii2. I want to remove index.php and \'?\' from url parameters. Which section needs to be edited in Yii2 for that?

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 13:09

    config/web.php

    $params = require __DIR__ . '/params.php';
    $db = require __DIR__ . '/db.php';
    
    
    $config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'aliases' => [
    '@bower' => '@vendor/bower-asset',
    '@npm'   => '@vendor/npm-asset',
    ],
    'components' => [
    'assetManager' => [
    // override bundles to use local project files :
    'bundles' => [
    'yii\bootstrap4\BootstrapAsset' => [
    'sourcePath' => '@app/assets/source/bootstrap/dist',
    'css' => [
    YII_ENV_DEV ? 'css/bootstrap.css' : 'css/bootstrap.min.css',
    ],
    ],
    'yii\bootstrap4\BootstrapPluginAsset' => [
    'sourcePath' => '@app/assets/source/bootstrap/dist',
    'js' => [
    YII_ENV_DEV ? 'js/bootstrap.js' : 'js/bootstrap.min.js',
    ]
    ],
    ],
    ],
    
    'request' => [
    // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
    'cookieValidationKey' => 'V_Pj-uMLTPPxv0Be5Bwe3-UCC6EjGRuH',
    'baseUrl' => '',
    ],
    
    'formatter' => [
    'dateFormat' => 'dd/MM/yyyy',
    'decimalSeparator' => ',',
    'thousandSeparator' => '.',
    'currencyCode'      => 'BRL',
    'locale'        => 'pt-BR',
    'defaultTimeZone'   => 'America/Sao_Paulo',
    'class'         => 'yii\i18n\Formatter',
    ],
    'datehelper' => [
    'class' => 'app\components\DateBRHelper',
    ],
    'formatcurrency' => [
    'class' => 'app\components\FormatCurrency',
    ],
    'request' => [
    // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
    'cookieValidationKey' => '123456',
    
    ],
    'cache' => [
    'class' => 'yii\caching\FileCache',
    ],
    'user' => [
    'identityClass' => 'app\models\User',
    'enableAutoLogin' => true,
    ],
    'errorHandler' => [
    'errorAction' => 'site/error',
    ],
    'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    // send all mails to a file by default. You have to set
    // 'useFileTransport' to false and configure a transport
    // for the mailer to send real emails.
    'useFileTransport' => true,
    ],
    'log' => [
    'traceLevel' => YII_DEBUG ? 3 : 0,
    'targets' => [
    [
    'class' => 'yii\log\FileTarget',
    'levels' => ['error', 'warning'],
    ],
    ],
    ],
    'db' => $db,
    
    'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => true,
    'rules' => [
    '' => 'site/index',                                
    '//' => '/',
    ],
    ],
    
    ],
    'params' => $params,
    ];
    
    if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
    'class' => 'yii\debug\Module',
    // uncomment the following to add your IP if you are not connecting from localhost.
    //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
    
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
    'class' => 'yii\gii\Module',
    // uncomment the following to add your IP if you are not connecting from localhost.
    //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
    }
    
    return $config; 
    

    arquivo .htaccess na pasta raiz

    
    Options +FollowSymlinks
    RewriteEngine On
    
    
    RewriteCond %{REQUEST_URI} ^/.*
    RewriteRule ^(.*)$ web/$1 [L]
    RewriteCond %{REQUEST_URI} !^/web/
    RewriteCond %{REQUEST_FILENAME} !-f [OR]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ web/index.php
    
    

    .htaccess dentro da pasta web/

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

提交回复
热议问题