Custom Base Url Codeigniter - codeigniter

in config.php , its possible to make another variable and call it in view
$root="http://".$_SERVER['HTTP_HOST'];
$root.=str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
$config['img_url']=$root.'assets/img/';
$config['jsctrl_url']=$root.'application/JsController/';
and call it like
<script type="text/javascript" src="<?php echo jsctrl_url();?>Account/register.js"></script>

What you need is a constant and not a function like base_url.
Head on to your config/constants.php and create a new one like:
$root ="http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
define('IMG_URL', $root . 'assets/img/');
define('JS_URL', $root . 'application/JsController/');
Then in your views you would use it as a normal constant.
<script type="text/javascript" src="<?php echo JS_URL ?>Account/register.js"></script>

Related

How to integrate CDN in existing codeigniter Project

How to integrate content delivery network in existing codeigniter Application.
So that base-url needs to point main server and
resource-url (like images, css, js etc) need to point cdn server.
All images are linked with absolute path like below
<img src="<?php echo base_url("media/images/image_name.jpg"); ?>" alt="StackOverflow" >
And the Result will be like below
<img src="http://example.com/media/images/image_name.jpg" alt="StackOverflow" >
Here i need to replace example.com with cdn.example.com.
Do i need to edit manually in all places?
Thanks for Helping.
It is like normal call in view
e.g.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
We need to create one helper method like below
function cdn_base_url() {
$url = base_url();
if(is_cdn_integrated){
$url = 'http://cdn.example.url.com/';
}
return $url;
}
And we need to call that method in view files like below instead of calling with base_url().
<img src="<?php echo cdn_base_url()."media/images/image_name.jpg"; ?>" alt="StackOverflow" >

Joomla Template Image uploader for logo

I am trying to build a joomla template and would like to include an image upload option for users to upload custom images for a logo.
Currently I have this structure:
In my templateDetails.xml:
<config>
<fields name="params">
<fieldset name="basic">
<field name="logofile" type="media"
directory="images"
label = "Logo"
description ="Choose a logo"/>
</fieldset>
</fields>
</config>
Then in my index.php:
// Getting params from template
$params = JFactory::getApplication()->getTemplate(true)->params;
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
$this->language = $doc->language;
$this->direction = $doc->direction;
// Detecting Active Variables
$option = $app->input->getCmd('option', '');
$view = $app->input->getCmd('view', '');
$layout = $app->input->getCmd('layout', '');
$task = $app->input->getCmd('task', '');
$itemid = $app->input->getCmd('Itemid', '');
$sitename = $app->getCfg('sitename');
$logo = $this->params->get('logo');
Then further down in my body I try to output the $logo variable but It displays nothing on the screen....
<a href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/<?php echo $logo;?>
Anyone know where I might be going wrong?? Much appreciated for any help or advice.
EDIT
Made some progess.
when I upload images they are being stored in the default images folder in the joomla directory. I copied the image across to my templates image folder and managed to get it to display using this:
<img src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/images/logo2.png"
however it is not dynamic as $logo is not being used. It does not seem to be assigned any value at all.

How can include component js after templete js

I added javascript on templete index.php
$doc = JFactory::getDocument();
$doc->addScript($this->baseurl . '/templates/' . $this->template . '/js/jquery.js', 'text/javascript');
and added another on component below
$document = JFactory::getDocument();
$document->addScript($this->baseurl . '/templates/' . $this->template . '/js/validation.js');
but always js (validation.js) of component getting add before tempelete js(jquery.js)
How can i add component js(validation.js) after templete js(jquery.js).
If you are using jquery.js as main jQuery library on your project then you can simple include the jquery.js before the head module in index.php. like below.
<script src="templates/js/jquery.js" type="text/javascript"></script>
<jdoc:include type="head" />
Then in your component you can simply use same as below.
$document = JFactory::getDocument();
$document->addScript($this->baseurl . '/templates/' . $this->template . '/js/validation.js');
An alternate option is Addcustom tag , helps to adding scripts to the document.
$document = JFactory::getDocument();
$document->addCustomTag('<script src="'.$this->baseurl . '/templates/' . $this->template . '/js/validation.js" type="text/javascript"></script>');
Otherwise you have to load the component js also in index.php with second order but that will load the js file for all pages.
Hope its helps..
I am going through google and found one solution that i can use custom tag function for component like below:
$document = JFactory::getDocument();
$document->addCustomTag('<script src="'.$this->baseurl . '/templates/' . $this->template . '/js/validation.js" type="text/javascript"></script>');
and for template as it below:
$doc = JFactory::getDocument();
$doc->addScript($this->baseurl . '/templates/' . $this->template . '/js/jquery.js', 'text/javascript')

php file exists returns nothing always - CodeIgniter

I'm wanting to display images. The user can add up to 7 images, but they can also add less than 7 if needed.
I searched around and saw that file_exists() is the function I'm after, but I've tried a couple of ways and it always gives me no image.
Here's one way I tried:
<?php if(file_exists(base_url() . "photos/" . $p['p_id'] . "_5.jpg")):?>
<div class="single">
<a href="<?php echo base_url();?>photos/<?php echo $p['p_id'];?>_5.jpg" rel="lightbox[image]">
<img src="<?php echo base_url();?>photos/<?php echo $p['p_id'];?>_5.jpg" style="width:100px; height:100px;"/>
</a>
</div>
<?php endif; ?>
Then I searched around and found out that its not a good idea to define the path inside the function, so I defined it outside:
<?php $path = base_url() . "photos/" . $p['p_id'] . "_";?>
<?php $img4 = $path . "4.jpg";?>
<?php if(file_exists($img4)):?>
which didn't work either.I'm testing with 4 images being in the folder 'photos' so I know there should be 4 images being displayed. Any ideas on how I should fix it would be great. Thanks
The function file_exists accepts file path as its parameter and not the url like what you're using now which is CodeIgniter's base_url().
You should use the FCPATH constant which points to your codeigniter app's base directory.
This is what you should assign to your path (depending on where your image files are located in your application directory):
<?php $path = FCPATH . "htdocs/images/" . $product[0]['product_id'] . "_";?>
But for some reason if really you need to use the base_url(). You can use get_headers() which checks the headers returned by the url in an associative array and false on failure. From the headers returned you can determine whether the image url is valid or not. More about get_headers().
Hope it helps.
Cheers,
Ardy
File exists requires a directory path not a URL, a URL will always return false.
<?php
for($i=1; $i < 7; $i++):
{
$img = $path . $i . ".jpg";
$url=getimagesize($img);
if(!is_array($url))
{
?>
<div class="single">
<a href="<?php echo base_url();?>images/<?php echo $product[0]['product_id'];?>_<?php echo $i; ?>.jpg" rel="lightbox[productimage]">
<img src="<?php echo base_url();?>images/<?php echo $product[0]['product_id'];?>_<?php echo $i; ?>.jpg" style="width:100px; height:100px;"/>
</a>
</div>
<?php
}
}
?>
Be forewarned this is a touch on the slow side, but it does work with URL's on the server.

How can I add custom code to the header

How can I add the following line to the header, in a component?
<!--[if lt IE 9]><script language="javascript" type="text/javascript" src="js/excanvas.min.js"></script><![endif]-->
Not sure if it will work with any text, but you could try:
$document =& JFactory::getDocument();
$document->addCustomTag('<!--[if lt IE 9]><script language="javascript" type="text/javascript" src="js/excanvas.min.js"></script><![endif]-->');
I hope it will be helpfull.
You can use :
<?php
$document = &JFactory::getDocument();
$document->addScript( '/js/excanvas.min.js' );
?>
I am searching for how to add the conditional statement though ...
UPDATE
Checking if the user agent is IE
<?php
$document = &JFactory::getDocument();
//Is it Internet Explorer?
ereg('MSIE ([0-9]\.[0-9])',$_SERVER['HTTP_USER_AGENT'],$reg);
if(isset($reg[1])) {
//Yes, it is Internet Explorer
$document->addScript( '/js/excanvas.min.js' );
}
?>

Resources