I'm new to codeigniter. I'm trying to make a simple site, with three (3) conditional views.
For example:
If "user_agent" detects mobile device -> load mobile_view
else load -> web_view
and if "site" settings is disabled [value = 0] load -> maintenance_view
I have the following code but doesn't work. It always load the maintenance view.
Controller:
function index() {
$this->load->library('user_agent');
if($this->agent->is_mobile())
{
$this->load_mobile();
} else {
$this->load_web();
}
}
public function load_web() {
$site = $this->Datamodel->getsetting();
if(isset($site) && $site==1) { //check if site settings is enabled [(if site "value == 1" load -> web_view) ELSE (site "value == null" load -> maintenace_view)]
$this->load->view('web_view');
} else {
$this->load->view('maintenance_view');
}
}
public function load_mobile() {
$this->load->view('mobile_view');
}
Model for site settings:
function getsetting() {
$this->db->select("site");
$this->db->from('configuration');
$query = $this->db->get();
$ret = $query->row();
return $ret->site;
}
For site settings conditions, I have extendted my old code from here How to load a view based on condition in Controller (Codeigniter)
The previous code only have 2 conditions if site is enabled [site value == 1] or not [site value == null]
Depending on conditions, web or index view is loaded in controller if site "value == 1" otherwise if site "value == null" load the maintenace view
Now Im trying to add a mobile view, but I cant figure it out how, with the rest of the code.
function index() {
if(site_value == 0){
$this->load->view('maintenance_view');
}else{
$this->load->library('user_agent');
if($this->agent->is_mobile())
{
$this->load_mobile();
} else {
$this->load_web();
}
}
}
Related
In cakePHP 4
I have a controller and view.php connected with it.
I can use a route like this: sitename.com/projects/45, where 45 - is sample project ID.
Using this url I can reach a page with the content of particular project. But If I want to construct something like a page of settings of this project, how I have to do it?
For example via url sitename.com/projects/45/settings
Help please
It's simple:
// sitename.com/projects/45
// public function view($id) { ... }
// sitename.com/projects/45/settings
public function view($id, $passed = null) {
if($passed == 'settings') {
// do ...
}
}
or
public function view($id) {
$passed = $this->getRequest()->getParam('pass');
if (in_array('settings', $passed)) {
// do ...
}
}
Sorry for my English, but what I'm trying to say is explained below.
I have a controller say ControllerCard which has an action like this.
function actionScanCard()
{
...
$this->redirect('/transaction/redeem');
...
}
In other controllers, ControllerTransaction, I am trying to get that it comes/redirected from /card/scan-card
function actionRedeem()
{
$redirectFrom = ????;
if ($redirectFrom === '/card/scan-card')
{
// some actions
}
else
throw new ForbiddenHttpException('Must scan card!');
}
How do I get this $redirectFrom value with Yii2?
You could use the remember() & previous() methods in yii\helpers\BaseUrl.
function actionScanCard()
{
...
\yii\helpers\Url::remember();
$this->redirect('/transaction/redeem');
...
}
in TransactionController (or other)
function actionRedeem()
{
$url = \yii\helpers\Url::previous();
if($url === Url::to('card/scan-card')) {
// some actions
} else{}
}
Im using laravel 4.0 im tyring to display a layout only if a variable ==0 (just in case a user tries to navigate to the url instead of clicking through) (i know I can redirect instead of extending but this is undesirable for now)
I am trying to get the layout to only extend when the user navigates to the page manually, noajax is set to true if their is no ajax request being sent when it goes to the function, so if the user where to navigate to the url manually it will still display the page but extend the layout.
#if ($noajax==1)
#extends('layouts.master')
#endif
#section('content')
//controller
public function test($id,$model)
{
if (Request::ajax())
{
//$foreign_key and $model must be <> null
if ($id == null || $model == null) {
$this->render('../Errors/missing_arg', 'error');
return;
}
if($model=="ArtObj")
{
$partable = "art_objects";
$path='img/art-objects/';
}
$parid=$id;
$noajax=0;
$mediaimgs = Media::where('parent_id' , $id )->where('parent_table', $partable)->paginate(15);
$response = Response::Json($mediaimgs);
return View::make('/Admin/manageimage/manage_image',compact('parid','mediaimgs','model','path','noajax'));
}
else{
if($model=="ArtObj")
{
$partable = "art_objects";
$path='img/art-objects/';
}
$parid=$id;
$mediaimgs = Media::where('parent_id' , $id )->where('parent_table', $partable)->paginate(15);
$response = Response::Json($mediaimgs);
$noajax = 1;
return View::make('/Admin/manageimage/manage_image',compact('parid','mediaimgs','model','path','noajax'));
}
}
In this case you should use 2 views in controller.
In controller you should use:
if ($noajax) {
return View::make('noajax');
}
else {
return View::make('ajax');
}
In noajax view you can extend from any other view and if noajax and ajax have common code, you should put it in separate file and use #include in those both views to include common part of code.
I have a base class which is inherited by all the controllers. I am having a function in the base class which determines the logged in users role using Auth. Once the users role is determine a variable $LoggedIn_role is set.
This method is correctly called on the initial page load, but later i am issuing ajax calls to check whether the user is still logged in, at that time the Auth::logged_in() always returning 0.
The kohana version i am using is 3.3
Can any one please suggest what is the best approach to circumvent this issue. Thanks.
To login -
if ($ValidObj->check()) {
if (Auth::instance()->login($_POST['email'], $_POST['password'],FALSE)) {
$this->DoPostLoginJobs();
} else {
$this->ManageError(Controller_Application::MsgWrongUserCredentials);
}
} else {
$this->Form_Errors = $ValidObj->errors('');
}
To Logout -
public function action_logout() {
$loggedout = Auth::instance()->logout();
if ($loggedout)
HTTP::redirect ('/home/'); // redirects to the home page.
}
Inside the controller_Application . The base class of all the controllers
public function DetermineUserRole() {
$this->LoggedIn_Role = Controller_Application::None;
try {
if (Auth::instance()->logged_in('freelancer')) {
$this->LoggedIn_Role = Controller_Application::Freelancer;
$this->LoggedIn_Id = Auth::instance()->get_user()->pk();
} else if (Auth::instance()->logged_in('employer')) {
$this->LoggedIn_Role = Controller_Application::Employer;
$this->LoggedIn_Id = Auth::instance()->get_user()->pk();
}
} catch (Gettrix_Exception $exc) {
$this->ManageError(Controller_Application::RedirectNonRecoverableError);
}
public function before() {
if ($this->request->is_ajax()) {
$this->auto_render = false;
}
$this->DetermineUserRole();
if($this->auto_render==TRUE){
parent::before();
$this->template->content = '';
$this->template->styles = array();
$this->template->scripts = array();
View::set_global('site_name', 'TheWebTeam');
View::bind_global('Form_Errors', $this->Form_Errors);
View::bind_global('LoggedIn_Role', $this->LoggedIn_Role);
View::bind_global('LoggedIn_Id', $this->LoggedIn_Id);
View::bind_global('InvitedEmail', $this->InvitedEmail);
View::bind_global('InvitedUniqueID', $this->InvitedUniqueID);
View::bind_global('scripts', $this->template->scripts);
View::bind_global('styles', $this->template->styles);
}
//This is inside the Home page controller, where it lists all the jobs for the logged in user.
public function action_joblist()
{
echo Auth::instance()->logged_in() . //The state holds to the initial state, doesn't //change when the user is logged out or logged in.
}
Please note that action_joblist() is called via AJAX/Jquery call.
The issue is fixed by following the instructions given in the link : http://forum.kohanaframework.org/discussion/9619/session-timeout-corruption-problems/p1
i am using opencart in one project,
every thing is working fine but i am unable to find an option to stop view of home page until LOGIN.
actually this is project requirement, no one can see home until LOGIN.
is there way to do this with OPEN CART ?
Thanks
this is untested, but should point you in the right direction:
(OpenCart 1.4.9.x)
Save this to:
catalog/controller/common/check_login.php
<?php
class ControllerCommonCheckLogin extends Controller {
public function index() {
if (!$this->customer->isLogged()) {
// Require to be logged in
$ignore = array(
'account', 'payment'
);
$match = false;
if (isset($this->request->get['route'])) {
foreach ($ignore as $i) {
if (strpos($this->request->get['route'], $i) !== false) {
$match = true;
break;
}
}
}
// Show site if logged in as admin
$this->load->library('user');
$this->registry->set('user', new User($this->registry));
if (!$this->user->isLogged() && !$match) {
return $this->forward('account/login');
}
}
}
}
?>
Edit /index.php
Find:
// Maintenance Mode
$controller->addPreAction(new Action('common/maintenance/check'));
Add After:
// Login Check
$controller->addPreAction(new Action('common/check_login'));
Essentially use the same logic as the maintenence check... The big difference is it looks for the word 'account' in the string... If it finds it it allows the page to be displayed, if not it redirects to the login page...
Use the word "account" instead of "login" in case they need to register... All of the account pages already check for loggin so there is no worry there...
Again, untested so you may need to tweak one or two things - but it should/may work by dropping in the code
check_login.php for opencart 1.5.3
<?php
class ControllerCommonCheckLogin extends Controller {
public function index() {
// Require to be logged in
if (!$this->customer->isLogged()) {
// Require to be logged in
$ignore = array(
'account','payment'
);
$match = false;
if (isset($this->request->get['route'])) {
foreach ($ignore as $i) {
if (strpos($this->request->get['route'], $i) !== false) {
$match = true;
break;
}
}
}
// Show site if logged in as admin
$this->load->library('user');
$this->user = new User($this->registry);
if (!$this->user->isLogged() && !$match) {
$this->redirect($this->url->link('account/login'));
}
}
}
}
?>
There is nothing built-in that I know of, but you can do it yourself. Based on your answers to #CarpeNoctumDC's questions you may have to do some digging, but this should get you started:
system/library/customer.php
public function isLogged() { ... }
catalog/controller/common/home.php
if (!$this->customer->isLogged()) {
// login page
exit;
}
The right way to go about this is to open
/catalog/controller/common/home.php
find public function index() { at the top of the code, and after it put
if(!$this->customer->isLogged()) {
$this->session->data['redirect'] = $this->url->link('common/home');
$this->url->redirect('account/login', '', 'SSL');
}
In case you're unsure how to do this properly, just take a look at the first few lines after public function index() { in
/catalog/controller/account/account.php
setting your code in the home controller to common/home in place of account/account