У меня есть 2 проекта (A использует Yii2 и B, используя простой PHP).
Я хочу, чтобы B получал содержимое файла из A с помощью file_get_contents
.
Но каждый раз, когда B запрашивает A, он всегда дает мне страницу входа.
Вот мой код...
AuthController.php
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['service'],
'rules' => [
[
'actions' => ['service'],
'allow' => true,
'roles' => ['@'],
],
],
],
];
}
public function beforeAction($action)
{
if ($action->id == 'service')
Yii::$app->controller->enableCsrfValidation = false;
return parent::beforeAction($action);
}
public function actionService()
{
$status = 200;
$status_header = 'HTTP/1.1 ' . $status . ' ' . $this->_getStatusCodeMessage($status);
$content_type="application/json; charset=utf-8";
header($status_header);
header('Content-type: ' . $content_type);
header('X-Powered-By: ' . "Nintriva <nintriva.com>");
echo json_encode(["hello","w"]);
}
private function _getStatusCodeMessage($status)
{
$codes = Array(
200 => 'OK',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
500 => 'Internal Server Error',
501 => 'Not Implemented',
);
return (isset($codes[$status])) ? $codes[$status] : '';
}
}
b.php
$url = 'http://localhost/now/basic/web/index.php?r=auth/service';
$result = file_get_contents($url);
print_r($result);
Что-то не так? Спасибо
измените правила. сделать это так
'rules' => [
[
'actions' => ['service'],
'allow' => true,
],
],
просто удалите
'roles' => ['@'],
или просто добавьте это
'roles' => ['?','@'],
AccessControl
для ваших действий ...