URL路由-yii-basic-app-Yii2 [英] URL Routing - yii-basic-app - Yii2

查看:125
本文介绍了URL路由-yii-basic-app-Yii2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在URL中传递ID。我很难做到这一点。因此,我正在浏览



在我得到这个之后错误,我在 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 +>'=>'/',
),
],

但是,现在的问题是每个页面都重定向到index.php页面。



什么去做 ?我不知道。请帮助我纠正此问题。

解决方案

您必须添加 .htaccess web 文件夹中的c>文件,用于从URL中删除 index.php



  
上的RewriteEngine

#如果是目录或文件存在,直接使用请求
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
#否则将请求转发到index.php
RewriteRule。 index.php


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.

解决方案

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

这篇关于URL路由-yii-basic-app-Yii2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆