I would like to redirect a customer to the login page if they are not logged in from any page on the site. I am trying to limit access to a subdomain to a specific customer
The other possibility is to create a preAction - e.g. like maintenance mode. I have used this once and I think this is much cleaner solution than implementing it in the view template (so it follows the MVC pattern - logic is done in controller, view is only for presenting the data and gathering input from user).
Create a class catalog/controller/common/login.php
class ControllerCommonLogin extends Controller {
public function index() {
if($this->config->get('config_store_id') == 1) { // if desired store, continue checking
if(!$this->customer->isLogged()) { // Check user isn't logged in
if(empty($this->request->get['route']) || $this->request->get['route'] != 'account/login') { // Redirect if route isn't account/login
$this->redirect($this->url->link('account/login', '', 'SSL'));
}
}
}
}
}
Then open up index.php (frontend one) and find line:
// Maintenance Mode
$controller->addPreAction(new Action('common/maintenance'));
and after add this:
// Login needed pre-action
$controller->addPreAction(new Action('common/login'));
You should be done.
Assuming you want to check the store is the subdomain one as well, you should use code something like this
// Check store ID against subdomain store id value
if($this->config->get('config_store_id') == 123) {
// Check customer isn't logged in
if(!$this->customer->isLogged()) {
// Redirect if route isn't account/login
if(empty($this->request->get['route']) || $this->request->get['route'] != 'account/login') {
$this->redirect($this->url->link('account/login', '', 'SSL'));
}
}
}
I would recommend checking if you are already on the login page, if so don't run code. You can check this if your on a page called login.php. Simple and clean solution
You need to add a condition like:
if($this->config->get('config_store_id') == 1) { //Add your subdomain store id, if you want to limit the condition for a subdomain.
if( (!isset($this->request->get['route']) || $this->request->get['route'] != 'account/login' ) && (!$this->customer->isLogged()) ){
$this->redirect($this->url->link('account/login', '', 'SSL'));
}
}
$this->request->get['route'] - this will get you the current page path.