Access Silverstripe Page Requirements from a controller - php

I have a the following in mysite/code/Page.php:
...
class Page_Controller extends ContentController {
public function init() {
parent::init();
Requirements::javascript("themes/example/js/global1.js");
Requirements::javascript("themes/example/js/global2.js");
Requirements::javascript("themes/example/js/global3.js");
}
}
...
and then a custom controller in mysite/controllers/PlayController.php:
...
class PlayController extends ContentController {
public function init() {
parent::init();
Requirements::javascript("themes/example/js/play.js");
}
}
...
I would like to use the Requirements from Page.php in PlayController.php. Since they both extend ContentController, is this possible?
A little background; PlayController is used to display a page for a theatre play with the template Layout/PlayPage.ss. If I put the Requirements in templates/Page.ss, they are inherited by PlayPage.ss. But I'd like to put the Requirements in a controller, so I can use Silverstripe's combine_files feature. Hope this makes sense!
Thanks :-)

You could create a subclass of ContentController that can serve as the base-class for both of your controllers. Eg. you create a MyController extends ContentController and then PlayController and Page_Controller extend MyController.
Or you could just require all the files in both controllers… if that's too much redundancy, you could also use the config API for this. Here's an example:
class Page_Controller extends ContentController
{
private static $js_requirements = [
"themes/example/js/global1.js",
"themes/example/js/global2.js",
"themes/example/js/global3.js"
];
public function init() {
parent::init();
foreach ($this->config()->js_requirements as $js) {
Requirements::javascript($js);
}
}
}
And then in your PlayController, you could access the config of Page_Controller like so:
class PlayController extends ContentController
{
public function init() {
parent::init();
foreach (Config::inst()->get('Page_Controller', 'js_requirements') as $js) {
Requirements::javascript($js);
}
Requirements::javascript("themes/example/js/play.js");
}
}
You could then also use the YAML config to configure your requirements, eg.:
# mysite/_config/config.yml
Page_Controller:
js_requirements:
- "themes/example/js/global4.js"
Note: Always use dev/build, so that your config variables are being picked up.

Related

phalcon controller indexAction break down

I am new into Phalcon framework. I just got the basic idea about it. Every controller has methods with multiple specific actions. I wrote a huge indexAction method but now I want to break it down with multiple private method so that I can reuse those functionality. But when I try to create any method without action suffix, it returns error(Page Not Found). How can I break it down into multiple methods?
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
$this->someMethod();
}
public function someMethod()
{
//do your things
}
}
Controllers must have the suffix “Controller” while actions the suffix “Action”. A sample of a controller is as follows:
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
}
public function showAction($year, $postTitle)
{
}
}
For calling another method, you would use it straight forward
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
echo $this->showAction();
}
private function showAction()
{
return "show";
}
}
Docs.
What exactly do you want? The answer seems trivial to me.
class YourController extends Phalcon\Mvc\Controller
{
// this method can be called externally because it has the "Action" suffix
public function indexAction()
{
$this->customStuff('value');
$this->more();
}
// this method is only used inside this controller
private function customStuff($parameter)
{
}
private function more()
{
}
}

PHP - How to extend and access parent constructor properties

I am attempting to access the parent class __construct properties within a child class that extends this, however not sure how to do this as I have tried multiple methods and didn't give me the expected result.
So I have a baseController and a indexController which extends it, I want to be able to have direct access to the properties of the parent within the child controller.
$config = ['site' => 'test.com'];
class baseController {
public function __construct($config){
$this->config = $config;
}
}
class indexController extends baseController {
public function __construct(){
parent::__construct(); // doesnt seem to give any outcome
}
public static function index() {
var_dump($this->config); // need to access within this method
}
}
$app->route('/',array('indexController','index')); // the route / would call this controller and method to return a response
There are several issues with code you have there. You are setting up config as a global, it should be inside your BaseController and set it to public or protected:
class BaseController {
protected $config = ...
Just like #mhvvzmak1 mentioned, your child constructor is calling the parent properly. for example you can do it like so:
class IndexController extends BaseController {
public function __construct(){
$config = [];
parent::__construct($config);
}
and finally just like dan08 mentioned, you can't reference $this from a static method, change your index function:
public function index() {
Update
If you really want the child function to remain static as required by your framework, you make config a static function on the BaseController and call it in the child.
class BaseController {
protected static function config() {
return ['site' => 'mySite'];
}
}
class Child extends BaseController {
public static function index() {
$config = BaseController::config();
}
}

Yii controller inheritance ( in submodule )

My question is, how can I inherit Controllers action in YII, like:
class MainController extends FController
{
public function ActionIndex()
{
$this->render("index"); //the view;
}
}
--------------------------------------------
class SecondController extends FController
{
public function ActiondIndex()
{
MainController::ActionIndex();
}
}
Actually in my case the SecondController is the DefaultController of a sub-module. I want to make single code based webpage.
Since PHP does not support multiple inheritance, you may access the base class via the parent keyword.
class MainController extends FController
{
public function ActionIndex()
{
$this->render("index"); //the view;
}
}
class SecondController extends FController
{
public function ActionIndex() // Note: you mistyped the name of this action in your example
{
parent::ActionIndex();
}
}
Although you cannot reach the grandparent's ActionIndex method directly, you have to create a workaround for that.

How to create two parent controllers in Codeigniter?

I want to create two parent controllers: one for admin and one for user site. They have to extend a regular Controller class but each of them has to do different things.
I wrote up an article showing how you do this.
http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
You need to create an __autoload() function in your config.php or directly include the base controller above the class definition.
This is pretty easy. Do the following:
Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
Open MY_Controller.php and add your multiple classes, like so:
class Admin_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
var_dump("from Admin_Parent");
}
}
class User_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
var_dump("from User_Parent");
}
}
Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php
Open adminchild.php and create your controller code, make sure to extend the name of the parent class, like so:
class Adminchild extends Admin_Parent {
function __construct() {
parent::__construct();
}
function test() {
parent::test();
}
}

How to 'copy' a Zend Framework Action without duplicating it?

I have two controllers which have some actions that are really the same.
How do I refer to the identical action in another controller?
class UserController extends Zend_Controller_Action {
public function listAction() {
//do something here
}
}
class AdminController extends Zend_Controller_Action {
public function listAction() {
//how to call UserController::listAction here?
}
}
What do I put in AdminController::listAction above so that I only have to write the code in UserController::listAction?
thanks
I would use a controller action helper, that way if you ever have to do the same thing again you can reuse it.
class My_Controller_Action_Helper_Whatever
{
public function direct()
{
return $this;
}
public function doSomething($paramA, $paramB)
{
// code
return $whatever;
}
}
Then implement in your controllers:
class UserController extends Zend_Controller_Action
{
public function someAction()
{
$this->getHelper('Whatever')->doSomething($a, $b);
}
}
class AdminController extends Zend_Controller_Action
{
public function anotherAction()
{
$this->getHelper('Whatever')->doSomething($a, $b);
}
}
You could do:
class baseController extends Zend_Controller_Action {
// common controller actions
public function listAction() {
// do stuff
}
}
class AdminController extends baseController {
// admin controller specific actions
}
class UserController extends baseController {
// base controller specific actions
}
You could also forward the request to the other controller by using:
class AdminController extends Zend_Controller_Action {
public function listAction() {
$this->_forward('list','user');
}
}
or if you would prefer the URL to change:
class AdminController extends Zend_Controller_Action {
public function listAction() {
$this->_redirect('/user/list');
}
}
You can forward to another action - simply specify the action, controller, module and params.
Parameters default to values of the current request, i.e. if you're in the default module, the code below will redirect to the listAction of UserController in the default module.
class AdminController extends Zend_Controller_Action {
public function listAction() {
//call UserController::listAction
return $this->_forward('list', 'user');
}
}

Categories