What's Codeigniter's absolute url? (for image src) - image

I have a javascript script that changes an image's src when clicked. When I had the same application in pure php without Codeigniter I simply had the images' src be something like "images/image_name" where images was the images folder, but now I'm using Codeigniter I don't know how to do it. I can't use php inside the JS script obviously so can't use the base_url() or site_url() functions or anything, so I suppose only the absolute path would work. And my images are in an images folder that's inside an assets folder which is at the root (i.e. at the same level as the Application, System and User_guide folders). So what should the images' src be? Thanks.

You can use php code inside script.
<script>
var imgSrc="<?php echo base_url('assets/images/image_name.jpg'); ?>";
</script>
Or you can also declare a global variable in header file to store base path.
<script>
basePath='http://www.yourdomain.com/';
</script>
and it can be used as
var imgSrc=basePath+"assets/images/image_name.jpg";

in addition to piyush's answer, I need to mention something that will help to use site_url() & base_url() properly.
site_url("controller/method/args") == base_url() + $config['index_page'] + /controller/method/args. You should use this to access pages.(Reference)
base_url("public/img/logo.png") == base_url() + /public/img/logo.png. You should use this to access resources such as images, stylesheets etc.(Reference)
Main difference between both functions is this part $config['index_page']. If you have already removed the $config['index_page'] from the config.php and you are using .htaccess to rewrite the URLs, then both function will result the same. But it is a best practice to use the correct function for the correct purposes.
Hope this helps.

Related

Laravel Blade: The image pointed by the html src attribute isn't compiled within the public directory, unlike the css url property. Why?

I must show an img html element with src pointing to an image that is originally defined within the resources/images/ folder. So I have written, in my Blade template, the following line (normally it's correct):
<img class="illustration" src="{{ asset('/images/design_7.jpg') }}" alt=""/>
Problem : it doesn't work. Indeed, I don't find this image in the directory named public.
I've seen that Laravel automatically compiles the image if the latter is pointed by the CSS url property. Why isn't it the case with the html src attribute? How can I solve this problem?
Use mix.copyDirectory('resources/<your folder images>', 'public/<your destination folder images>) in your webpack.mix.js file.
(The css compiled images were in fact due to mix.less(<the css file>, public/<the css destination file>))
See 1) https://laravel.com/docs/8.x/mix#copying-files-and-directories and 2) https://laravel.com/docs/8.x/mix#working-with-stylesheets

Laravel Vue - How to make vue to look for images in public folder

I am trying to load images from the public folder in the vue components. The asset helper doesn't work in vue , so I need to use the format
<img :src="'img/ic_add-sm.svg'" >
But instead of looking for the images in the public folder , vue is appending the current URL to the image path. For example , if the url is www.example.com/posts
it adds www.example.com/posts/img/ic_add-sm.svg
instead of www.example.com/img/ic_add-sm.svg
Add a forward slash to the beginning of your image path.
<img :src="'/img/ic_add-sm.svg'">
Since you don't appear to be doing anything special you should be able to just use
<img src="/img/ic_add-sm.svg">
At first bind the value of image source
<img :src="getPhoto() + variableName" />
Then create the function under your methods section and simply return the directory location, that's all.
getPhoto(){
return 'images/';
}
Note that, You cant declare directory location directly to :src attribute.

Integrate Codeigniter and Filterable

I'm trying to integrate Codeigniter and Filterable but I cant find much information about it. I have a page with many results and I want to filter the results, for example by the column "name" or "id". I have worked with Filterable before, but never had to do it with Codeigniter.
I tried to put the files on the root. Also in the folder "Application", even tried putting it on the same folder as the view. But in any of these cases I cannot link the files and get it to work.
The ways I've been trying to link the scripts are the following:
<script src="<?php echo site_url('src/filterable.js')?>"></script> This was with the scripts in application folder
<script src="./src/filterable.js"></script> Scripts on root
<script src="<?php echo site_url('admin/src/filterable.js')?>"></script> Scripts on view folder
Is there another way to achieve this? Or maybe another library I could use for it?
Here is how I like to deal with assets.
I create an helper called assets_helper in application/helpers/
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('js'))
{
function js($nom)
{
return '<script src="' . base_url() . 'assets/js/' . $nom . '.js" type="text/javascript" ></script>';
}
}
//This is only the part that handle js as it is what's bothering you
Here is the full helper I use : http://pastebin.com/ujETEXJ4
After that, at the same level as index.php I create those folder :
|-Application
|-System
|-index.php
|-Assets
|- css
|- sass
|- images
|- js
Put all the js file you need in your new js folder.
In my application/config/autoload.php I add my new helper
$autoload['helper'] = array('assets', ...);
Finally, in the footer of my page(s) :
<?= js('myjsfile'); ?> //I did not forgot the extension, it's how it works :)
Which will give at the end :
<script src="http://www.example.com/assets/js/myjsfile.js" type="text/javascript" ></script>
Thanks AdrienXL for your answer. I tried to follow your suggestion but in the end I couldn't make it work. For some reason Codeigniter didn't recognize the files I tried to reach.
So after a long searching I ran across a library called Datatables http://www.datatables.net/ it has the option to load the scripts locally or remotely, so just added the link to the scripts needed to the type of filter I had to implement and in the end this last option was what allowed me to make the filters work.

How to override Joomla System Messages - message.php template

Joomla by default renders its system messages (errors, notices etc.) in
libraries/joomla/document/html/renderer/message.php.
For my own template, I would like to customize the way these messages are displayed. However it does not seem to be possible in the traditional way, using template overrides.
Is anyone here aware of a way to accomplish something like this?
For Joomla! 1.7 - 2.5
You need copy libraries/joomla/document/html/renderer/message.php in to templates/YOUR_TEMPLATE/html/message.php
Then in index.php of YOUR_TEMPLATE you need to include the file (as it is not automatically included like other overrides):
// Message overwrite
require_once JPATH_ROOT .'/templates/'. $this->template .'/html/message.php';
Now you can safety overwrite the JDocumentRendererMessage::render() function there ;)
For Joomla! 3.x
You just need make html/message.php file in YOUR_TEMPLATE. This file should contain function renderMessage(). Check the isis default template, as example.
Templates overrides only work with the MVC - i.e. views and module chrome.
Without hacking the core all you can do is control what HTML tags are wrapped around the <jdoc:include type="message" /> tag in the template and the CSS defined for the elements of the message block.
A more elegant way to include your override in the template directory is to include the file in a system plugin:
public function onAfterInitialise() {
$app = JFactory::getApplication();
if ($app->isSite()) {
$template = $app->getTemplate();
if (!class_exists('JDocumentRendererMessage') && file_exists(JPATH_THEMES . '/' . $template . '/html/message.php')) {
require_once JPATH_THEMES . '/' . $template . '/html/message.php';
}
}
return true;
}
http://extensions.joomla.org/extensions/style-a-design/popups-a-iframes/26551
OR
http://extensions.panchsoft.com/product/1-popup-system-messages.html
Use this extension for default messages for Joomla.

base_url() not getting translated

I have inherited an app that was built with Code Igniter. I've set up the environment and loaded the app on a new machine and I'm having an issue with the call to base_url() not working.
Here is the code as it is in the view:
Start Study Analysis »
When the page is rendered, this is the URL that is created:
Start Study Analysis »
It appears that the function call is not taking place and the code is being inserted as plain text. I've looked at the config.php file and the base_url is set in there. The helper is being loaded in the autoload.php file and I've even tried to load the helper in the view.
Does anyone have any idea as to why this is happening?
Here is the code as it is in the view:
a href="< ?= base_url();?>index.php/study/start">Start Study Analysis » /a>
Either this is a formatting mistake in your post, or you just have a really mangled link there.
I'm going to take a guess that you do have a space between your < and ?=, which would indeed cause the code not to be parsed as php, but instead as broken text/html.
%20 is a url encoded space character, so it makes sense that this is what you'd see in your url.
Make sure there are no spaces in your PHP opening tag:
<?php echo base_url(); ?> and not < ?php echo base_url(); ?>
<?= base_url(); ?> and not < ?= base_url(); ?>
There isn't a space there. I needed to add it [for formatting the post]
The only other thing I can think of is that short tags are not enabled, but I admit that I do not know if this would be your result if that was the case. Try using <?php echo instead of <?=, or enabling the rewrite_short_tags option in your config.php. This certainly is not an issue specific to base_url() or anything similar, your php tags are getting parsed as plain text so what's inside them does not matter at all.

Resources