New to classes, and I'm having an issue dynamically changing a page's title. I think I may know what the issue is, but I don't really know what to do in order to fix the problem. I have two pages, admin.php (where the class is housed) and display-admin.php (displays what the user sees). Within admin.php this is what I have:
class Admin {
public $title;
public function login() {
require("login-form.php");
return $this->title = "Login";
}
...
}
$o = new Admin();
This is what I have within display-admin.php:
<?php
include_once("admin.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
if(isset($o->title)) {
echo "<title>My Site | " . $o->title . "</title>";
} else {
echo "<title>My Site</title>";
}
?>
</head>
<body>
<?php
...
switch($action) {
case "login":
$o->login();
break;
default:
$o->displayPages();
}
...
?>
</body>
</html>
The title always stays as My Site. It never adds the title needed. Now, when I tested echoing $o->title at the end (before the <\body> tag), it displayed the string I passed. Is this because I am trying to echo the variable before calling the function? If so, how do I fix in order to display the title, and have my content displayed within the body?
Thanks in advance for the any/all help/suggestions.
In your case it isnt work because $o->title doesnt exist.
It is created in your "login()" method, and this is after your
if(isset($o->title)) {
So you can do this:
class Admin {
public $title;
public function __construct() {
$this->title = "Admin";
}
}
You can do it per example in the constructor of your Admin Class.
The constructur is a magic method. It will be automatically called directly if you created your class.
Hope this will help you.
If you create an object from class Admin the title will be change into 'Admin'
In your Admin Page you can do this:
$o = new Admin();
echo "<title>".$o->title."</title>";
Related
I have several index pages
And I put the header page separately
I want the name of each page to be sent to the header
How can I do it, thank you?
model::setnameisset('home');
and model.php page
function setnameisset($name)
{
return $name;
}
Now I want it to be saved in the header page instead of the title tag
<!DOCTYPE html>
<html lang="en">
<head>
<base href="<?= URL ?>">
<meta charset="utf-8">
<!-- <meta http-equiv="Content-Type" content="text/html"> -->
<title></title>
</head>
Now how do I call that function?
See I have several pages like contact page or home page
And I put the header in a page and included it in these pages
The structure is m v c
I just want to send the name of the page or the keyword tag to the header so that it opens with that name during execution.
For example, my index or contact file
<?php model::nametag('homepage'); ?>
<body id="page-top">
</body>
Now I have a model file in the core folder
My controller file is like this
class Controller
{
function __construct()
{
}
function view($viewurl,$data=[],$noincludeheader = '',$noincludefooter = ''){
if ($noincludeheader =='') {
require_once ('views/share-base/header.php'); //panel header
}
require_once ('views/'.$viewurl.'.php'); //panel main page
if ($noincludefooter =='')
{
require_once ('views/share-base/footer.php'); // panel footer
}
}
function model($modelUrl){
require_once ('models/model_'.$modelUrl.'.php');
$classname='model_'.$modelUrl;
$this->model = new $classname;
}
}
And my model file is like this
public function nametag($name)
{
return $name;
}
It's so easy, I just want to be placed in any file, a name should be passed to the header page, I don't know how to use the function.
I have an opencart site, and I'm trying to configure facebook share options of my products.
Since everything is loaded as a separate module, I can't set the facebook meta tags like this (header.tpl):
<meta property="og:description" content="<?php echo $description; ?>" />
Because the $description doesn't exist while the header module is loading.It is created in the controller of product module. So I tried to change the content value dynamically (product.tpl):
$("meta[property='og:description']").attr('content','<?php echo $description; ?>');
And it worked, I can see that the value is changed (in the page source), then I debugged my page but I couldn't get the value.. I think I know the reason, I have to set the value before the page load but I'm not sure how can I do that.. do you have any idea ?
You could use the Document class to add those facebook tags (as many as you want). Just add two extra methods setFacebookDescription and getFacebookDescription, so you have to add the followings:
<?php
class Document {
private $facebook_description;
public function getFacebookDescription() {
return $this->facebook_description;
}
public function setFacebookDescription($facebook_description) {
$this->facebook_description = $facebook_description;
}
}
On each controller, you will find at the end of each method, a call which loads the header of Opencart, something like this $data['header'] = $this->load->controller('common/header'); (example). Note that it might differ from yours, it depends on Opencart version.
Now, in header.php controller you add:
<?php
class ControllerCommonHeader extends Controller {
public function index() {
$data['facebook_description'] = $this->document->getFacebookDescription();
}
}
this will get the facebook_description variable and pass it to the view header.tpl. Next, add the facebook tags between your <head> tags in header.tpl file:
<!DOCTYPE html>
<head>
<?php if ($facebook_description != '') { ?><meta property="og:description" content="<?php echo $facebook_description; ?>" /><?php } ?>
</head>
Finally, you can set facebook_description in each controller, by calling $this->document->setFacebookDescription('my description');.
Example: in product.php controller you add
<?php
class ControllerProductProduct extends Controller {
public function index() {
// code...
$this->document->setTitle($product_info['meta_title']);
$this->document->setDescription($product_info['meta_description']);
$this->document->setFacebookDescription($product_info['meta_description']);
// the rest of the code...
}
}
here you set the $product_info['meta_description'] as facebook description tag, however you could also use $product_info['name'] or other variable.
Final note: you can change all the Opencart system classes with the vqmod.
I want to access session values from layout.xml.
The code I did is
Layout.xml
<h2><?php $myprofile=new Zend_Session('user_session');
print $myprofile->username; ?> </h2>
Index Controller/index action
$userid = $this->_user->getUserId($username,$password);
$session = new Zend_Session_Namespace('user_session');
$session->username = $username;
$session->password = $password;
$session->uid = $userid;
$this->_redirect('home');
Home Controller/index action
$this->session = new Zend_Session_Namespace('user_session');
$this->view->uname = $this->session->username;
Home/index.phtml
<?php echo "The User Name is ".$this->uname?>
But it shows an error
Fatal error: Call to protected Zend_Session::__construct() from context 'Zend_View' in/var/www/shoppingcart/application/layouts/scripts/layout.phtml on line 19
I am able to get the session values in Home/index.html.
Expecting positive help.
Why using Zend_Session in the layout and Zend_Session_Namespace in the view ?
also maybe you should'n use sessions in the view/layout but pass it as param from the controller or the bootstrap file
I agree with Amine here, putting the Zend_Session in the view script is generally considered bad practice. I would indeed put it in the bootstrap as well, something like the following...
// Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initUserSession()
{
// If not already done, bootstrap the view
$this->bootstrap('view');
$view = $this->getResource('view');
// Initialise the session
$session = new Zend_Session_Namespace('user_session');
// See if the username has been set, and if not at
// least make the variable
if (isset($session->username)
$view->username = $session->username;
else
$view->username = null;
}
}
Then in the layout you can do this:
<?php if ($this->username !== null) : ?>
The User Name is <?php echo $this->username ?>
<?php else : ?>
No username has been set.
<?php endif; ?>
I'm using latest codeigniter and I need to create a flag (ideally in the config) when turned to 'true', all pages display a 'maintenance mode' message instead of executing their controller code.
What is the best/simplest practice for doing this?
Here my solution, works fine for me:
The below will call maintanance.php immediately, so you can go ahead and break your CI code without the world seeing it.
Also allow you to add you own ip address so that you can still access the site for testing etc.
In index.php add at top:
$maintenance = false; ## set to true to enable
if ($maintenance)
{
if (isset( $_SERVER['REMOTE_ADDR']) and $_SERVER['REMOTE_ADDR'] == 'your_ip')
{
##do nothing
} else
{
error_reporting(E_ALL);
ini_set('display_errors', 1); ## to debug your maintenance view
require_once 'maintenance.php'; ## call view
return;
exit();
}
}
Add file Maintanance.php in same folder as index.php (or update path above):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Maintenance</title>
<style>
body {
width:500px;
margin:0 auto;
text-align: center;
color:blue;
}
</style>
</head>
<body>
<img src="images/home_page_logo.png">
<h1><p>Sorry for the inconvenience while we are upgrading. </p>
<p>Please revisit shortly</p>
</h1>
<div></div>
<img src="images/under-maintenance.gif" >
</body>
</html>
<?php
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600');
?>
Extend the CI_Controller by putting a new file in your core directory called MY_Controller.
In this file's constructor, do something like this:
public function __construct()
{
parent::__construct();
if($this->config->item('maintenance_mode') == TRUE) {
$this->load->view('maintenance_view');
die();
}
}
Let all controllers in your app inherit from that class.
Here my solution, simply, clean and effectively for all urls calls and SEO respects:
Add this variables in:
application/config/config.php
$config['maintenance_mode'] = TRUE;
$config['maintenance_ips'] = array('0.0.0.0', '1.1.1.1', '2.2.2.2');
Add this conditional at the end of:
application/config/routes.php
if(!in_array($_SERVER['REMOTE_ADDR'], $this->config->item('maintenance_ips')) && $this->config->item('maintenance_mode')) {
$route['default_controller'] = "your_controller/maintenance";
$route['(:any)'] = "your_controller/maintenance";";
}
Create method maintenance in:
application/controllers/your_controller.php
function maintenance() {
$this->output->set_status_header('503');
$this->load->view('maintenance_view');
}
Create view:
application/views/maintenance_view.php
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
Here is what I've come up with for creating a maintenance mode.
Enable Hooks in the config.php file
Create an error_maintenance.php page under errors folder
Create a Hook called maintenance
In the hooks config setup your hooks call to run on post_controller
application/errors/error_maintenance.php
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
<style>Style your page</style>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
application/hooks/maintenance.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class maintenance
{
var $CI;
public function maintenance()
{
$this->CI =& get_instance();
$this->CI->load->config("config_maintenance");
if(config_item("maintenance"))
{
$_error =& load_class('Exceptions', 'core');
echo $_error->show_error("", "", 'error_maintenance', 200);
exit;
}
}
}
application/config/hooks.php
$hook['post_controller'][] = array(
'class' => 'maintenance',
'function' => 'maintenance',
'filename' => 'maintenance.php',
'filepath' => 'hooks',
'params' => array()
);
how about this :
create auto-loaded libraries which always check maintenance flag on your database.
create a module for controlling your application maintenance flag.
create a module for redirecting when maintenance mode is on
auto-loaded libraries can contain something like this :
class Maintenance_mode {
function __construct(){
$CI =& get_instance();
$check_maintenance = $CI->db->select('flag_mode')->get('tbl_settings')->result();
if($check_maintenance[0]->flag_mode == '1')
redirect(site_url('maintenance_mode_controller'));
}
}
next step is to create a controller for maintenance page.
this one works well,
application/views/vw_maintenance.php
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
<style>Style your page</style>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
<?php exit(); ?>
the exit() function is very importantn don forget to put it at the bottom, it will prevent all pages from being displayed.
application/libraries/maintenance.php
class Maintenance{
private $CI;
public function __construct() {
$this->CI =& get_instance();
// flag on and off
$this->flag( $this->CI->uri->segment(1) );
// get the flag status
$check_maintenance = $this->CI->db->select('flag_mode')->where(array('setting_name' => 'maintenance'))->get('tbl_settings')->result();
//display view if true
if($check_maintenance[0]->flag_mode == '1')
$this->CI->load->view('vw_maintenance');
}
private function flag($command){
$this->CI->db->where('setting_name', 'evolving');
switch($command){
case "activate":
$this->CI->db->update('tbl_settings', array('flag_mode' => 1) );
break;
case "deactivate":
$this->CI->db->update('tbl_settings', array('flag_mode' => 0) );
redirect(site_url('/'));
break;
}
}
}
autoload the library so it will be check every page load.
now you can activate and deactivate maintenance mode by typing or
I think this was easy, Just call view on the constructor like below.
comment (site will be live)
class home extends CI_Controller {
public function __construct() {
parent::__construct();
//echo $this->load->view('maintenance','',true);exit;
}
}
uncomment (site will be under maintenance)
class home extends CI_Controller {
public function __construct() {
parent::__construct();
echo $this->load->view('maintenance','',true);exit;
}
}
And you can use your own "maintenance.php" page under "view" in CI Application.
in AppController:
function beforeFilter() {
$company = 'name of Company';
$this->set(compact('company'));
}
in Controller class:
function companyinfo() {
$logo = '<div><?php $this->Html->image('logo'); ?></div>';
$welcome = 'welcome to $$company!';
$this->set(compact('logo','welcome'));
}
function beforeFilter() {
parent::beforeFilter();
}
in View class:
<html>
<body>
<?php echo $logo; ?>
<?php echo $welcome; ?>
</body>
</html>
it doesn't answer variable in view after passing the variable from AppController via controller..
1) When you use $this->set(compact('company'));, it is NOT setting a variable for use in any controller - it's passing $company to the view.
2) You're trying to write PHP code in a string, using a Helper (which are only available in Views)
$logo = '<div><?php $this->Html->image('logo'); ?></div>';
3) It's unusual to want to pass data from AppController to Controller to View.
What you probably want to do is something like this:
//App Controller
function beforeFilter() {
$company = 'name of Company';
$this->set(compact('company'));
}
//Controller
function companyinfo() {
$logo = 'logo';
$this->set(compact('logo'));
}
//Layout file (or view file, but I assume it's layout since you're getting data in the AppController)
<?php
echo '<div>' . $this->Html->image($logo) . '</div>';
echo "Welcome to " . $company;
I mean this in the most constructive way possible (we've all been there). It seems like you're struggling with some general PHP concepts. Before you get too heavy into CakePHP, I recommend trying out a few lengthy tutorials in generic PHP - then when you feel completely comfortable with it, dive into CakePHP.
To get company into the view you can set it in your appcontroller if you want it avaiable in ALL views throughout your site or inside a specific controller if you only want it available in the view of the function you set it inside of. Either way you'll need to make correct use of the set function. For example:
$this->set('company', 'Name of Company');
OR
$company = 'Name of Company';
$this->set('company', $company);
Afterwards you'll be able to access the $company variable in the view.
echo $company; outputs Name of Company
As for your question for Dave:
<?php $welcome = 'Welcome to $$company'; ?> <html><body><?php echo $welcome; ?></body></html>
would be written as:
<html><body><?php echo "Welcome to ". $company; ?></body></html>
However, you should really look into using layouts with cakephp so you don't need to the <html>, <head>, <body>, etc. tags in every view file