[英]Yii2 restful not working
通過使用此鏈接,我嘗試擁有一個寧靜的應用程序,但該應用程序無法正常工作,並始終返回以下內容:
找不到對象!
在此服務器上找不到請求的URL。 如果您手動輸入網址,請>>檢查拼寫,然后重試。
如果您認為這是服務器錯誤,請與網站管理員聯系。 錯誤404本地主機Apache / 2.4.4(Win32)OpenSSL / 1.0.1e PHP / 5.5.1
我這樣稱呼它: localhost/restful/web/users
編輯:
這種調用也不起作用: localhost/restful/web/?r=users
它返回了這個:
找不到(#404)找不到頁面。
Web服務器正在處理您的請求時,發生了以上錯誤。
如果您認為這是服務器錯誤,請與我們聯系。 謝謝。
這是我的config/web.php
:
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'urlManager'=>[
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
],
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'a',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
],
'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' => require(__DIR__ . '/db.php'),
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}
return $config;
和控制器:
<?php
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController{
public $modelClass = 'app\models\User';
}
也是由gii創建的模型。
通過啟用curl並在我的控制器中使用它解決了問題。
有相同的錯誤(在每個請求上均未找到404)解決了將.htaccess添加到(我的yii路徑)/ web文件夾(在具有index.php的位置)的問題:
# use mod_rewrite for pretty URL support
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
這是因為當您創建一個Yii2的干凈實例時,您沒有.htaccess文件(我認為這可能是安全原因)。
對我來說:(通過curl啟用並在控制器中使用它)是通過composer安裝的
composer require --prefer-dist linslin/yii2-curl "*"
在控制器中使用
<?php
namespace app\controllers;
use yii\rest\ActiveController;
use linslin\yii2\curl;//this was added
class UserController extends ActiveController
{
不要工作 也許這可以幫助某人:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.