Last active
November 30, 2019 21:06
-
-
Save AugustMiller/1a24a377fd1d4cff2c680c404bc2fdce to your computer and use it in GitHub Desktop.
Basic Craft/Yii Module + Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'*' => [ | |
'modules' => [ | |
'my-module' => \modules\Module::class | |
], | |
'bootstrap' => [ | |
'my-module' | |
] | |
], | |
// ... Probably more application config, here! | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace modules; | |
use Craft; | |
/** | |
* Custom module class. | |
* | |
* This class will be available throughout the system via: | |
* `Craft::$app->getModule('my-module')`. | |
* | |
* You can change its module ID ("my-module") to something else from | |
* config/app.php. | |
* | |
* If you want the module to get loaded on every request, uncomment this line | |
* in config/app.php: | |
* | |
* 'bootstrap' => ['my-module'] | |
* | |
* Learn more about Yii module development in Yii's documentation: | |
* http://www.yiiframework.com/doc-2.0/guide-structure-modules.html | |
*/ | |
class Module extends \yii\base\Module | |
{ | |
/** | |
* Initializes the module. | |
*/ | |
public function init() | |
{ | |
// Set a @modules alias pointed to the modules/ directory | |
Craft::setAlias('@modules', __DIR__); | |
// Set the controllerNamespace based on whether this is a console or web request | |
if (Craft::$app->getRequest()->getIsConsoleRequest()) { | |
$this->controllerNamespace = 'modules\\console\\controllers'; | |
} else { | |
$this->controllerNamespace = 'modules\\controllers'; | |
} | |
parent::init(); | |
// Custom initialization code goes here... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace modules\controllers; | |
use craft\web\Controller; | |
/** | |
* This controller should be placed at `modules/controllers/TestController.php`! | |
*/ | |
class TestController extends Controller | |
{ | |
/** | |
* @var array Actions to allow anonymous (unauthenticated) access to. | |
*/ | |
public $allowAnonymous = ['my-test']; | |
/** | |
* Test action, accessible via `?action=my-module/test/my-test` (or with the `action` body param) | |
*/ | |
public function actionMyTest() | |
{ | |
// Anything you need to do with the incoming data, including sending remote requests: | |
// $myParam = Craft::$app->getRequest()->getBodyParam('myPostValue'); | |
return 'Hello, world!'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment