In my web application using codeigniter. I am trying to use base_url() function but it shows empty results. I have also used autoload helper through autoload file, but then too it doesn't seem to work. Also I had defined base constants but all in vain.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
<script type="text/javascript">
//<![CDATA[
base_url = '<?= base_url();?>';
//]]>
</script>
</head>
In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php (on or around line 67):
$autoload['helper'] = array('url');
Or, manually:
$this->load->helper('url');
Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:
echo base_url();
Remember also that the value returned is the site's base url as provided in the config file. CodeIgniter will accomodate an empty value in the config file as well:
If this (base_url) is not set then CodeIgniter will guess the protocol, domain and path to your installation.
application/config/config.php, line 13
If you want to use base_url(), so we need to load url helper.
By using autoload $autoload['helper'] = array('url');
Or by manually load in controller or in view $this->load->helper('url');
Then you can user base_url() anywhere in controller or view.
First of all load URL helper. you can load in "config/autoload.php" file and add following code
$autoload['helper'] = array('url');
or in controller add following code
$this->load->helper('url');
then go to config.php in cofig folder and set
$config['base_url'] = 'http://urlbaseurl.com/';
hope this will help
thanks
Check if you have something configured inside the config file /application/config/config.php e.g.
$config['base_url'] = 'http://example.com/';
I think you haven't edited codeigniter files to enable base_url(). you try to assign it in url_helper.php you also can do the same config/autoload.php file. you can add this code in your autoload.php
$autoload['helper'] = array('url');
Than You will be able to ue base_url() like this
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
Apart from making sure you have set config/autoload.php:
$autoload['helper'] = array('url');
Change application/config/config.php from:
$config['base_url'] = 'http://example.com/';
Become a dynamic base url:
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https": "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
And using the host from php to run it on local, below is just an example port.
php -S localhost:2000
If you don't want to use the url helper, you can get the same results by using the following variable:
$this->config->config['base_url']
It will return the base url for you with no extra steps required.
Load url helper in controller
$this->load->helper('url');
Anything if you use directly in the Codeigniter framework directly, like base_url(), uri_string(), or word_limiter(), All of these are coming from some sort of Helper function of framework.
While some of Helpers may be available globally to use just like log_message() which are extremely useful everywhere, rest of the Helpers are optional and use case varies application to application. base_url() is a function defined in url helper of the Framework.
You can learn more about helper in Codeigniter user guide's helper section.
You can use base_url() function once your current class have access to it, for which you needs to load it first.
$this->load->helper('url')
You can use this line anywhere in the application before using the base_url() function.
If you need to use it frequently, I will suggest adding this function in config/autoload.php in the autoload helpers section.
Also, make sure you have well defined base_url value in your config/config.php file.
This will be the first configuration you will see,
$config['base_url'] = 'http://yourdomain.com/';
You can check quickly by
echo base_url();
Reference: https://codeigniter.com/user_guide/helpers/url_helper.html
Question -I wanted to load my css file but it was not working even though i autoload and manual laod why ? i found the solution =>
here is my solution :
application>config>config.php
$config['base_url'] = 'http://localhost/CodeIgniter/'; //paste the link to base url
question explanation:
" >
i had my bootstrap.min.css file inside assets/css folder where assets is root directory which i was created.But it was not working even though when i loaded ?
1. $autoload['helper'] = array('url');
2. $this->load->helper('url'); in my controllar then i go to my
I encountered with this issue spending couple of hours, however solved it in different ways. You can see, I have just created an assets folder outside application folder. Finally I linked my style sheet in the page header section. Folder structure are below images.
Before action this you should include url helper file either in your controller class method/__constructor files or by in autoload.php file. Also change $config['base_url'] = 'http://yoursiteurl'; in the following file application/config/config.php
If you include it in controller class method/__constructor then it look like
public function __construct()
{
$this->load->helper('url');
}
or If you load in autoload file then it would looks like
$autoload['helper'] = array('url');
Finally, add your stylesheet file.
You can link a style sheet by different ways, include it in your inside section
-><link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css" type="text/css" />
-> or
<?php
$main = array(
'href' => 'assets/css/style.css',
'rel' => 'stylesheet',
'type' => 'text/css',
'title' => 'main stylesheet',
'media' => 'all',
'index_page' => true
);
echo link_tag($main); ?>
-> or
finally I get more reliable code cleaner concept. Just create a config file, named styles.php in you application/config/styles.php folder.
Then add some links in styles.php file looks like below
<?php
$config['style'] = array(
'main' => array(
'href' => 'assets/css/style.css',
'rel' => 'stylesheet',
'type' => 'text/css',
'title' => 'main stylesheet',
'media' => 'all',
'index_page' => true
)
);
?>
call/add this config to your controller class method looks like below
$this->config->load('styles');
$data['style'] = $this->config->config['style'];
Pass this data in your header template looks like below.
$this->load->view('templates/header', $data);
And finally add or link your css file looks like below.
<?php echo link_tag($style['main']); ?>
This All Directly Access Function Will Be Loaded Through Helper Class Only.
Like URL, Security, File all Are Helpers and You can Also Load Custom Helpers.
config/autoload.php
$autoload['helper'] = array('url', 'file', 'authorization', 'custom');
Related
After migration, every page add password textfield at the end of every page.
What is this? How can I remove or troubleshoot?
Drupal 7.27 with apache 2.4 and php 7.0 (same problem with php 5.6.35).
Look there is a script loaded on these pages (just above the <form> itself) that creates the input tags and set the windows focus in it :
<script type="text/javascript">
var d = document;
d.write("<br><br><form method='post'><center><input type='password'...>...");
// ...
</script>
You want to remove this script.
Since there are several ways to include javascript with Drupal it may be difficult to spot the code responsible for that. Given the ugliness of the script itself, it could very well be harcoded in a theme template file (in this case, theme switching during migration would explain why your issue suddenly arose).
The chance is that ugly snippets like this is quite often hardcoded so you can make a search for a part of the js string (e.g. 'd.write("<br><br><form') in your project at the root of your site and/or in sites/all.
Lastly, find the guy that wrote this and beat him ;)
Your code is including a java script in every page which is creating input type password since it is included in every page that's why you are getting this field.
kindly check your requirement for same.
In drupal We can add JS in drupal by following method
1.)By drupal_add_js() function
drupal_add_js() is drupal api function to include js.
Example:
drupal_add_js('misc/collapse.js');
// add JS file
drupal_add_js('misc/collapse.js', 'file');
// For including inline javascript
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
//For including inline javascript and includ and includ it in footer
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', array(
'type' => 'inline',
'scope' => 'footer',
'weight' => 5,
));
//For including External JS
drupal_add_js('http://example.com/example.js', 'external');
//For passing php value to JS
drupal_add_js(array(
'myModule' => array(
'key' => 'value',
),
), 'setting');
Example:
drupal_add_js(drupal_get_path('module', 'mymodule') . '/mymodule.js');
for more infomation visit https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_js/7.x
2.)Adding by Form API
we can used '#attached' property of form api for including js
Example:
$form['#attached']['js'] = array(
drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
);
3.)Adding JS in info file
We can including javascript in script file
Example:
name = My theme
description = Theme developed by me.
core = 7.x
engine = phptemplate
scripts[] = mytheme.js
4.)By preprocess function
if we want to conditionaly include JS we can include it in preprocess function
function mytheme_preprocess_page(&$vars, $hook) {
if (true) {
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/mytheme.js');
$vars['scripts'] = drupal_get_js(); // necessary in D7?
}
}
To use standalone twig with standalone asset I use this code:
use Symfony\Component\Asset\Packages;
use Symfony\Component\Asset\PackageInterface;
use Symfony\Bridge\Twig\Extension\AssetExtension;
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\PathPackage;
use Symfony\Component\Asset\UrlPackage;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
use Symfony\Component\Asset\Context\RequestStackContext;
use Symfony\Component\HttpFoundation\RequestStack;
$versionStrategy = new EmptyVersionStrategy();
$namedPackages = array(
'css' => new PathPackage('twig/css', $versionStrategy, new RequestStackContext(new RequestStack())),
);
$defaultPackage = new Package($versionStrategy);
$packages = new Packages($defaultPackage, $namedPackages);
$twig->addExtension(new AssetExtension($packages));
It works fine, I use {{ asset () }} function but it adds a leading / in path like:
<link rel="stylesheet" type="text/css" href="/twig/css/mystyles.css">
that causes css file not found and not loaded. Without leading / it works fine.
How to fix it?
I am using twig-bridge to have asset in my twig as I am using both of them standalone.
My website is www.kipclip.com and it's running on Kohana. I created a new rental page. But it's not taking my CSS and JS files. I tried to find how this is included or if Kohana has a special method for do that. But still not successful. Do you have any idea regarding this?
A quick and dirty way to do so is to tack on the name of the script(s) and style(s) in the view you're implementing using regular html script and style tags and go on from there.
But, if you don't like quick and dirty, and prefer to do it better and more concrete, if you use Kohana 3.2, you can do the following. I haven't tried this on the older or newer versions, so it may or may not work in them (If you try to port it to that version, consult the transitioning document relative to the version in question that you wish to port):
/**
* /application/classes/controller/application.php
*/
abstract class Controller_Application extends Controller_Template {
public function before() {
parent::before();
if($this->auto_render) {
//Initialize empty values for use by ALL other derived classes
$this->template->site_name = '';//this is a psuedo-global set in this class
$this->template->title = '';//this too is set by the controller and action
$this->template->content = ''; //this is set by the controller and action
$this->template->styles = array();
$this->template->scripts = array();
$this->template->admin_scripts = array();
}
}
/**
* The after() method is called after your controller action.
* In our template controller we override this method so that we can
* make any last minute modifications to the template before anything
* is rendered.
*/
public function after()
{
if ($this->auto_render) {
//set the CSS files to include
$styles = array(
'style1', //the css file with all the defaults for the site
'jquery-library-css'
);
//set the JavaScript files to include
$scripts = array(
'myscript1',
'myscript2'
);
$admin_scripts = array(
'jquery-admin-functions',
);
//now, merge all this information into one so that it can be accessed
//by all derived classes:
$this->template->styles = array_merge($this->template->user_styles, $user_styles);
$this->template->scripts = array_merge($this->template->user_scripts, $user_scripts);
$this->template->admin_scripts = array_merge($this->template->admin_scripts, $admin_scripts);
}
//bind the site_name to the template view
$this->template->site_name = 'My Site Name';
//OLD WAY shown below:
View::set_global('site_name', 'My Site Name'); //set the site name
//now that everything has been set, use parent::after() to finish setting values
//and start rendering
parent::after();
}
}
So, how does this work? Remember that the application.php class is the base controller class from which all other controller classes are derived from. By implementing this type of binding to the base controller, every derived controller has access to what scripts, styles, etc. are available. And, as a result every associated view called by that controller also has access to those variables.
So, now to access those variables in your view:
Example, the template PHP file: /application/views/template.php
If it's defined like this (using PHP short tags - but do not use short tags in production code!):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html>
<head>
<meta charset='utf-8'/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<?php
/**
* Link the css files stored in the `static` folder from the project root.
* This will vary depending on how you have your files saved.
*/
foreach($user_styles as $style) : ?>
<link rel="stylesheet" href="<?php echo URL::base() . 'static/css/' . $style ?>.css" type="text/css"/>
<?php endforeach; ?>
<?php //Create AND set a dynamic page title - much like Facebook ?>
<title><?php echo $site_name; if(!empty($title)) echo ' - ' . $title; ?></title>
</head>
<body>
<!-- Fill in the body with HTML, PHP - whatever you want -->
<?php
/**
* Now, load the scripts:
* According to Yahoo, for better site performance, all scripts should be loaded after the body has been loaded
*/
foreach($user_scripts as $script) : ?>
<script src="<?php echo URL::base() . 'static/js/' . $script; ?>.js" type="text/javascript"></script>
<?php endforeach; ?>
</body>
</html>
There are two takeaway points from all of this:
One: If you want something to be global, or available to all controllers (and subsequent views), define them and bind them in the base application controller class.
Two: As a result, this functionality also gives you tremendous leverage and power in that if you have derived classes, you can them implement this same type of binding to that particular controller class making it available to any subsequent derived controller classes. That way, if you have two classes that should not have access to certain files and their associated functionality, e.g. an admin JavaScript file that loads all posts by some user, then this kind of implementation can make your life much, much, much easier.
And, a third hidden option is that give Kohana is PHP, you can use regular vanilla PHP / HTML in an associated view if you can't figure it out immediately. Although, that is something that I would dissuade you from doing in production code.
Whichever way, I hope this can assist you.
I need to access plugins directory in my customer javascript file. I know we have do have functions to retrieve plugins directory path using php function plugins_url().
However, I need this to retrieve in my js file where I have to put some images.
Any ideas??
PS: My js file is saved as a javascript file and therefore, I can't use php tags in it
Use <?php echo plugins_url(); ?> where you want to get the url in the js file.
For example:
var pluginUrl = '<?php echo plugins_url(); ?>' ;
It's actually quite easy...
In functions.php
wp_enqueue_script( 'main', get_template_directory_uri() . '/js/main.js', array('jquery'), '20151215', true );
$translation_array = array( 'templateUrl' =>get_stylesheet_directory_uri() ); //after wp_enqueue_script
wp_localize_script( 'main', 'jsVars', $translation_array );
then in the js-file:
var sitepath=jsVars.templateUrl+"/";
Edit: Just in case it's not understood completely:
The translation array is created and contains a key value pair
The array is then injected as jsVars (javascript variable) into the js scripts
So basically we (mis)use the translation array to inject variables to JS
with the wp_localize_script command.
off course instead of the templateUrl you can define all the params you want with PHP to inject their values into any js file you want.
In this example we inject the template Url into the main js script via jsVars, but it can be any variable into any js script
I've got the result just by assigning the real plugin url:
let pluginUrl = "../wp-content/plugins/YOUR_PLUGIN_FOLDER_NAME/";
I'm trying to figure out where the <head> for all of the pages in Drupal is (I'm using Orange theme if it matters). I have to add analytics code into the <head>.
Inside which file would I find the <head>?
Use drupal_set_html_head() by placing this in your themes template.php file. If the function MYTHEMENAME_preprocess_page() already exists insert what is inside the {closure} brackets below (before the $vars['head'] if that exists in it as well) :
function MYTHEMENAME_preprocess_page(&$vars, $hook) {
// say you wanted to add Google Webmaster Tools verification to homepage.
if (drupal_is_front_page()) {
drupal_set_html_head('<meta name="google-site-verification" content="[string from https://www.google.com/webmasters/verification/verification]" />');
$vars['head'] = drupal_get_html_head();
}
}
In template.php of your theme folder :
function your_theme_preprocess_html(&$variables) {
$appleIcon57px = array(
'#tag' => 'link',
'#attributes' => array(
'rel' => 'apple-touch-icon',
'href' => '/images/ICONE-57.png',
'type' => 'image/png',
'media' => 'screen and (resolution: 163dpi)'
)
);
drupal_add_html_head($appleIcon57px, 'apple-touch-icon57');
}
If you look in your theme folder you'll see page.tpl.php, that is the template for the site. You can add the code there most likely.
How to change a page meta description and keywords in Drupal 6
One another solution is to use blocks in header these can also managed very effectively using css.
All you have to do is to go to Structure->Blocks then create new block.
Then select theme corresponding to it and position in which section you want to show that block.
Custom html like can be added. And can be handled from that id.
It allow me to handle page structure very easily.
In the theme folder (e.g. themes/orange) there is a templates folder with the file html.tpl.php
In this file you can freely add what you need to the head section and it will be added to every page.
there is a google analyitics module that will accomplish this for you with just your key.