I have a custom route in my routes.yml that forwards any unknown request to BaseController
'$URLSegment/$Name/$Action/$ID': 'BaseController'
And from there the the request is routed (among other places) to my main Controller where it is handled by the index() of the controller.
However it will always use the index() so if I go to the URL test1/test2/action/5 it will still be run by index()
This is my BaseController
class BaseController extends ModelAsController {
public function getNestedController() {
$action
$params = $this->getRequest()->params();
$this->loadMain($params['URLSegment'], $params['Name'], $params['Action'], $params['ID']);
}
private function loadMain($first, $name, $action, $id) {
$main = new MainController();
$main->{$action}();
}
}
and this will call the function however the index() function has already run and set the template.
I know I could call the function from index() and return the template like that however I'm fairly sure this would bypass the security features of the allowed actions which I'm keen to maintain.
I've defined $allowed_actions in my MainController and have added:
private static $url_handlers = array(
'$URLSegment/$Name/something/$ID' => 'something'
);
but it still just calls index().
How can I maintain the same action routing of SilverStripe through the custom routed MainController
You need to define private static $allowed_actions and maybe private static $url_handlers also, the latter providing custom routing.
Basically all the things from: https://docs.silverstripe.org/en/3/developer_guides/controllers/routing/
This isn't well documented but if you are wanting to reverse the URL convention of /path/$Action/$Name/$ID to something like /path/$Name/$Action then you put:
private static $url_handlers = array(
'path//$Name/$Action' => 'handleAction'
);
The handleAction is important as it calls the parents (Controller`) handleAction and routes it correctly applying permissions and so forth.
Related
Like superficial descriped in SilverStripe Docs, I'm trying to set a custom controller for my homepage.
I changed the default homepage link to 'custom-home' and added those two routes.
The second one, with the path in it works and directs me to my controller. The first (empty) one just sends me to an 404-error page.
Couldn't figure out how to fix that. Any suggestions?
routes.yml
Director:
rules:
'': 'MyHome_Controller'
'custom-home': 'MyHome_Controller
_config.php
RootURLController::set_default_homepage_link('custom-home');
MyHome_Controller.php
<?php
class MyHome_Controller extends Page_Controller {
private static $allowed_actions = [];
private static $url_handlers = [];
public function init() {
parent::init();
}
public function Link($action = null) {
return Director::baseURL() . 'custom-home';
}
public function index() {
$data = [
'Title' => 'Hello World',
'ClassName' => __CLASS__,
];
return $this
->customise($data)
->renderWith([__CLASS__, 'Page']);
}
}
I believe the way the empty route (RootURLController) works is that you're telling it the URLSegment of a page in the CMS that should resolve to the root URL. So I think what you need to do is go into the CMS and change the URLSegment of your CustomHomePage to 'custom-home'.
I'm using SilverStripe 3.3.1 and have a custom route set up to handle urls with many parameters. That works.
However, the routing rule causes Page fields and functions to be inaccessible in the Page_Controller and templates. Any ideas how to fix this?
//MyPage class
class MyPage extends Page {
//Not accessible if route to controller specified in config.yml
private static $db = array(
'MyPageVar' => 'Int',
);
//Not accessible if route to controller specified in config.yml
public function getMySpecialVar() {
return $this->MyPageVar;
}
}
//MyPage_Controller class
class MyPage_Controller extends Page_Controller {
private static $allowed_actions = array(
'index',
'detailsearch',
);
private static $url_handlers = array (
'detailsearch/$Key1/$Value1/$Key2/$Value2/$Key3/$Value3/$Key4/$Value4/$Key5/$Value5' => 'detailsearch',
);
/**
* UseMyPageVar()
*
* #return Boolean
*/
public function UseMyPageVar() {
//Empty if route to controller specified in config.yml
Debug::show($this->MyPageVar);
Debug::show($this->Title);
Debug::show($this->Content);
//Error if route to controller specified in config.yml
Debug::show($this->getMySpecialVar());
return true;
}
}
MyPage.ss
<!-- This work as expected if no route is specified. -->
<!-- But all vars are empty if route is specified in config.yml -->
<p>MyVar: $MyPageVar</p>
<p>Title: $Title</p>
<p>Content: $Content</p>
Routing rule in config.yml
Director:
rules:
'mypage': 'MyPage_Controller'
This question is also posted on the Silverstripe forum:
http://www.silverstripe.org/community/forums/general-questions/editpost/413506
It's not pretty, but for now I've solved the problem by using a private var in the Controller class to hold a reference to the page.
//MyPage_Controller class
class MyPage_Controller extends Page_Controller {
private $_page; //reference to page that's lost with custom routing
//ContentController uses route, which has been changed to
// 'MyPage_Controller' by routing rule, to initialize
// page reference. Can't find anything so reference
// not set. (set to -1)
public function init() {
parent::init();
//Initialize using default route overwritten in routing rule
// This will break if URL segment changed in CMS
$route = array_search($this->URLSegment,
Config::inst()->get('Director', 'rules'));
$link = str_replace($this->URLSegment, $route, $this->Link());
$this->_page = $this->Page($link);
}
//Use private var to access page fields
public function MyPageVar() {
Debug::show($this->_page->MyPageVar);
}
//expose $Content to templates
public function Content() {
return $this->_page->Content;
}
//Can't use Title() so expose Page Title as $PageTitle
public function PageTitle() {
return $this->_page->Title;
}
}
Three things spring to mind when I look at your code:
That "mypage" in config.yml should be the name of a public method on MyPage_Controller. As it is, SilverStripe cannot find a matching method called mypage and will default to calling index() instead.
Routes should really go in a separate routes.yml file so you can "namespace" it to be invoked before or after SilverStripe's own core routes. If you don't do this, then it may result in the weird behaviour you're experiencing.
Did you know that you can debug your routes using the ?debug_request=1 URL param? See: https://docs.silverstripe.org/en/3.3/developer_guides/debugging/url_variable_tools#general-testing
Lets say I am building an OOP-based user authentication system, and I would like to incorporate the following principles: Direct Injection, Inheritance, Encapsulation, Polymorphism and the Single Responsibility Principle.
My background in programming is has always relied on procedural programming, and thus, am finding it difficult to really put these practices into correct use.
Assume I have these classes:
class Config
{
public function set($key, $value);
public function get($key, $default = null);
}
class User
{
public function __construct(PDO $dbh, $id = null);
public function setProfile(Profile $profile);
}
class Auth
{
public function __construct(Config $config);
public function login($username, $password, $keepLoggedIn = true);
public function isLoggedIn();
public function getLoggedInUser();
public function logout();
public function register(array $data);
}
class Session
{
public function start($sessionName = null);
public function write($key, $value);
public function read($key, $default = null);
}
class Profile
{
public function setAddress(Address $address);
public function setName($name);
public function setDOB(DateTime $date);
public function getAge();
}
class Validator
{
public function validate($input);
}
I have intentionally left off the function bodies to keep things simple.
To the best of my knowledge, I believe I'm using the principles correctly. However, I am still unclear as to how you would connect classes like: the Validator to the User model, the User model to the Auth and the Session to the Auth class. All of which depend on each other.
You are on the right track. The way these classes connect to each other is called extending. I tend to go towards an MVC setup, meaning Model, View, Controller.
Your logic goes into the controller, all your DB queries and concrete back end methods go in the model. The controller receives requests and returns responses. It's the middleman. It talks to the back end after a request has been made to it, and feeds the front in via response.
So you have a core controller (keep it bare minimal), then each class you make extends the core controller. So your controller is where you tie all this together.
<?php
//your main core controller, where you load all these things you need avilable, so long as this class is extended
class CoreController {
public $auth
public $session;
public $view;
function construct__ ()
{
$this->auth = instantiateAuthClassHere();
$this->session = instantiateSessionClassHere();
$this->view = instantiateViewClassHere();
}
public function anotherHelperForSomething(){
//helper stuff for this method
}
}
//index, page, or content controller, depending on how many you need, i.e. if you want a controller for each page, thats fine, e.g indexController, etc..
//this is the middle man, has logic, receives requst, returns response to view.
class Controller extends CoreController {
public function index (){
$userModel = new userModel();
//do something with this
$session = $this->session;
$content = 'some html';
$userInfo = $userModel->getUsers();
$view = $this->view->render( array(
'content' => $content,
'userInfo' => $userInfo,
));
return $view;
}
}
//Core LIbraries
class Validator {
//your validator stuff
}
//Core LIbraries
class Session {
//your validator stuff
}
//Core LIbraries
class Auth {
//your validator stuff
}
class CoreModel{
public $validator;
function __construct(){
$this->validator = instantiateValidatorClassHere();
}
}
//a user model class (back end). you want a model class for each db table pretty much.
class UserModel extends CoreModel {
// if you need the validator anywhere inside this class, its globally available here inside any class that extends the CoreModel, e.g. $this->validator->methodName()
public function getUsers (){
$sql = 'SELECT * from users';
$result = $db->get($sql);
return $result;
}
}
Notice, on the Controller, this is a generic name for something like indexController, or anything custom. Also, I have the word extends there. It inherits all the objects from the parent that it extends. Inside it, now they will be available via $this->. See my example where I get $this->session.
Try to avoid constructs - you probably don't need them anywhere except for the core, and under special circumstances, which you might then need to check for yourself before you do even that. I dont use constructs much anymore. It can be a bit clunky and unmanageable.
I'm setting up routing to a controller and I keep getting either a 404, or the 'getting started with the silverstripe framework' page.
In routes.yaml I have:
---
Name: nzoaroutes
After: framework/routes#coreroutes
---
Director:
rules:
'view-meetings/$Action/$type': 'ViewMeeting_Controller'
My controller looks like this:
class ViewMeeting_Controller extends Controller {
public static $allowed_actions = array('HospitalMeetings');
public static $url_handlers = array(
'view-meetings/$Action/$ID' => 'HospitalMeetings'
);
public function init() {
parent::init();
if(!Member::currentUser()) {
return $this->httpError(403);
}
}
/* View a list of Hospital meetings of a specified type for this user */
public function HospitalMeetings(SS_HTTPRequest $request) {
print_r($arguments, 1);
}
}
And I've created a template (ViewMeeting.ss) that simply outputs $Content, but when I flush the site cache and visit /view-meetings/HospitalMeetings/6?flush=1
I get the default 'getting started with the Silverstripe framework' page
I know the routing in routes.yaml is working, because if I change the route there and visit the old URL I get a 404, but the request doesn't seem to fire my $Action...
You had 2 different rules in your YAML and controller ($type vs $ID). Also, I don't think you need to define the route in both YAML and the Controller.
Try this, the YAML tell SS to send everything that starts with 'view-meetings' to your Controller, then $url_handlers tell the Controller what to do with the request depending on everything after 'view-meetings' in the URL.
routes.yaml
---
Name: nzoaroutes
After: framework/routes#coreroutes
---
Director:
rules:
'view-meetings': 'ViewMeeting_Controller'
ViewMeeting_Controller.php
class ViewMeeting_Controller extends Controller {
private static $allowed_actions = array('HospitalMeetings');
public static $url_handlers = array(
'$Action/$type' => 'HospitalMeetings'
);
public function init() {
parent::init();
if(!Member::currentUser()) {
return $this->httpError(403);
}
}
public function HospitalMeetings(SS_HTTPRequest $request) {
}
}
The Silverstripe documentation on routing isn't at all clear on this point, but for $Action to be correctly interpreted you should use a double slash before it in the routes.yml file:
view-meetings//$Action/$type
According to the documentation, this sets something called the 'shift point'. Exactly what this means isn't described very well either in the documentation or in the source code which matches URLs against rules.
I'm doing some guessing here, but what if you drop the
public static $url_handlers = array(
'view-meetings/$Action/$ID' => 'HospitalMeetings'
);
part and change the Action method to:
// View a list of Hospital meetings of a specified type for this
public function HospitalMeetings(SS_HTTPRequest $request) {
// Should print 8 if url is /view-meetings/HospitalMeetings/6
print_r($request->param('type');
}
I have this thing that I need in multiple places:
public function init()
{
$fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session
if(!$fbLogin->user) $this->_redirect('/'); #Logout the user
}
These two lines:
$fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session
if(!$fbLogin->user) $this->_redirect('/'); #Logout the user
Whats the best way to do it in ZendFramework?To create a plugin or? I mean I want to execute it in multiple places but If I need to edit it I want to edit it in one place.
Here is an example of an Action Helper that you can call from your controllers easily.
<?php
class My_Helper_CheckFbLogin extends Zend_Controller_Action_Helper_Abstract
{
public function direct(array $params = array())
{
// you could pass in $params as an array and use any of its values if needed
$request = $this->getRequest();
$view = $this->getActionController()->view;
$fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session
if(!$fbLogin->user) {
$this->getActionController()
->getHelper('redirector')
->gotoUrl('/'); #Logout the user
}
return true;
}
}
In order to use it, you have to tell the helper broker where it will live. Here is an example code you can put in the bootstrap to do so:
// Make sure the path to My_ is in your path, i.e. in the library folder
Zend_Loader_Autoloader::getInstance()->registerNamespace('My_');
Zend_Controller_Action_HelperBroker::addPrefix('My_Helper');
Then to use it in your controller:
public function preDispatch()
{
$this->_helper->CheckFbLogin(); // redirects if not logged in
}
It doesn't go into much detail, but Writing Your Own Helpers is helpful as well.
If you need this check in every Controller you could even set up a baseController from which you extend instead of the default one:
class My_Base_Controller extends Zend_Controller_Action
{
public function init()
{ ...
class IndexController extends My_Base_Controller
{ ...
Shift your init() into the base controller and you don't need to repeat yourself in every specific controller.
Need a varying init() in a specific controller?
class FooController extends My_Base_Controller
{
public function init()
{
parent::init();
...