URL Routing - yii-basic-app - Yii2

天大地大妈咪最大 提交于 2019-12-24 15:11:09

问题


i was trying to pass ID in URL. I was getting difficulty to do it. So, i was going through tutorials like Larry Ullman - URL Routing.

My problem is: I created confirm.php page and created a controller too but error like NOT FOUND (#404) coming.

SiteController.php

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\swiftmailer\Mailer;

use app\models\LoginForm;
use app\models\ContactForm;
use app\models\EntryForm;
use app\models\RegisterForm;
use app\models\LoginExecForm;
use app\models\ForgotPasswordForm;

class SiteController extends Controller
{
  .
  .
  public function actionConfirm($id)
    {
        $id = Yii::$app->request->get('id');
        return $this->render('confirm');
    }
}

config/web.php

<?php

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
    'urlManager' => [
      'showScriptName' => false,
      'enablePrettyUrl' => true,
        'enableStrictParsing' => false,
        'rules' => [
            '<controller>/<action:\w+>.<_format>' => '<controller>/<action>',
            'v1/<controller>/<action:\w+>.<_format>' => 'v1/<controller>/<action>',
        ],
    ], 

confirm.php

<?php

/* @var $this yii\web\View */

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
use yii\bootstrap\Modal;
use yii\helpers\Url;

$this->title = 'Register';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
    asd
</div>

Error Coming => Not Found(#404)

After i got this error, I added few codes in config/web.php

<?php

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
    'urlManager' => [
      'showScriptName' => false,
      'enablePrettyUrl' => true,
      'rules'=>array(
        '<controller:\w+>/<id:\d+>' => '/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '/',
        '<controller:\w+>/<action:\w+>' => '/',
        ),
    ], 

But, now problem is every page is redirecting to index.php page.

What to do ? I'm having No Idea. Please Help me to rectify this problem.


回答1:


You have to add .htaccess file in web folder, for removing index.php from URL.

Like as,

RewriteEngine on

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php



回答2:


    'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => false,
        'showScriptName' => false,
        'rules' => [
            '<controller>/<action:\w+>.<_format>' => '<controller>/<action>',
            'v1/<controller>/<action:\w+>.<_format>' => 'v1/<controller>/<action>',
        ],
    ],



回答3:


Add this code in main.php

  'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => false,
    'showScriptName' => false,
    'rules' => [
        '<controller>/<action:\w+>.<_format>' => '<controller>/<action>',
        'v1/<controller>/<action:\w+>.<_format>' => 'v1/<controller>/<action>',
    ],
],

and also add this code in .htaccessfile



来源:https://stackoverflow.com/questions/32947396/url-routing-yii-basic-app-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!