Не удается вызвать действие модуля Craft 3

#php #module #controller #homestead #craftcms

#php #модуль #контроллер #усадьба #craftcms

Вопрос:

Я пытаюсь настроить действие контроллера, которое может быть вызвано заданием cron. У меня есть контроллер, живущий внутри modules/chill/controllers/NotificationController , с действием actionIndex , запущенным в Homestead at http://chill.test .

При вызове URL http://chill.test/actions/chill/notifications/index -адреса я получаю экран входа в собственный браузер. Контроллер расширяет craftwebController и переопределяет $AllowAnonymous .

Контроллер выглядит следующим образом:

 <?php
namespace ChillControllers;

use Craft;
use craftwebController;

class NotificationsController extends Controller
{
    protected $allowAnonymous = true;

    /*
     * Call via: http://chill.test/actions/chill/notifications/index
     */
    public function actionIndex(){

        Craft::$app->getDeprecator()->log("Chill", "Testing the error logging on index action", 'notifications.log');

        return 'Welcome to the NotificationsController actionIndex() method';
    }
}
 

Также вызывая URL-адрес через Paw с настройкой application / json в заголовках, я получаю следующий 401-ответ:

 {
  "error": "Your request was made with invalid credentials."
}
 

Есть идеи о том, как обойти всплывающее окно входа в систему, или я неправильно это настроил?

Ответ №1:

Обычно я настраиваю модуль, а затем регистрирую маршруты, используя события Crafts

 private function _registerSiteRoutes()
{
     Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES,
         function (RegisterUrlRulesEvent $event) {
             $event->rules[] = [
                 'class' => 'yiirestUrlRule',
                 'controller' => ['api/' => 'my-module/api/v1/base'],
                 'extraPatterns' => [
                      'GET notifications' => 'notifications',
                 ],
             ];
         }
     );
}