xjx7514329 2015-12-07 15:32:33 3997次浏览 4条回复 0 0 0
第一步

首先我按照官方文档的指导,配置了web.php:

        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => true,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
            ],
        ]
第二步

然后我新建了一个controller

<?php
namespace app\controllers;
use app\models\User;
use yii\rest\ActiveController;

class UserController extends ActiveController {
    public $modelClass = 'app\models\User';

}

同时使用gii生成了model

第三步

我使用post测试http://localhost/users 发现报错,404

后来我这样写: 'http://localhost/index.php/users'

正确返回数据

最后,问题来了

我想把index.php隐藏掉,该怎么弄,求各位大大指教

  • 回复于 2015-12-07 21:44 举报

    配置.htaccess作转发。

    4 条回复
    回复于 2015-12-08 10:53 回复

    搞定了!就按这种方法,谢谢

    回复于 2015-12-08 10:57 回复

    之前错在哪?

    回复于 2015-12-08 11:05 回复

    第一,没配对.htaccess,这个文件要放到index.php同级目录,也就是web/下面,第二,如果本地环境是wamp的话,那么apache的根目录也要指向web/

    回复于 2015-12-08 11:15 回复

    必须跟index.php在同一目录,因为是针对这一目录做的设置。 apache根目录不指向web时可以通过补全路径去访问,如:127.0.0.1/frontend/web/index.php。 只是域名长些,效果一样。

  • 回复于 2015-12-07 21:45 举报

    .htaccess 参考内容

    RewriteEngine on
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # otherwise forward it to index.php
    RewriteRule . index.php
    
    1 条回复
    回复于 2015-12-08 10:05 回复

    配置了,在web目录底下,没有作用啊

  • 回复于 2016-01-19 16:17 举报

    高端啊。搜rest搜到这。

  • 回复于 2017-04-18 21:18 举报

    您好,
    方便问一下model该怎么写吗?不了解gii
    我安照文档,开始怎么都是404,然后看到你的,访问不是404了,变成500了
    所以想请教一下model应该怎么写 (我是只写了一个返回表名的方法)
    谢谢

您需要登录后才可以回复。登录 | 立即注册