Codeigniter doesn't show Images - image

I have a problem with CodeIgniter showing images in the function detail. In the function index, they are shown. Can someone show me where the problem is?
class Reference extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('reference_model');
}
public function index()
{
$data['work'] = $this->reference_model->getList();
$menu['page'] = "Reference";
$menu['pages'] = $this->pages->getPages();
$this->basic_template->set('title','Reference | DSVision');
$this->basic_template->menu('menu_view',$menu);
$this->basic_template->script('reference_script_view');
$this->basic_template->view('basic_script', 'reference_view',$data);
}
public function detail($number)
{
if($this->reference_model->exist($number) == 0)
redirect('reference');
$data['id'] = $number;
$data['work'] = $this->reference_model->getWork($number);
$menu['page'] = "Reference";
$menu['pages'] = $this->pages->getPages();
$this->basic_template->set('title','Reference | DSVision');
$this->basic_template->menu('menu_view',$menu);
$this->basic_template->view('basic', 'detail_view',$data);
}
}

This problem occurs when you give the image src as src="/images/logo.png".
When giving a path, do it like this:
src="<?php echo $this->config->item('base_url'); ?>/image/logo.png"
Take note that you must set the base_url in config.php inside the config folder.
This problem occured because the browser is trying to find the image directory inside your controller directory as if you are loading a function!

u have to put every images, js etc. in your root-dir of CI. Example:
/yourapp/css/style.css
In your view you write e.g. the following:
<link type="text/javascript" src="<? echo base_url() ?>css/style.css" />

Related

To link in CodeIgniter within application

<a class="navbar-brand" href="<?php echo base_url ('welcome/aboutus');?> "> About Us </a>
Here welcome is controller name and aboutus is page name. I have set autoload. If I run this then it shows "Object not Found", what's wrong with that?
Your question is a bit hard to help you with because you have limited code to go by.
You may need to set your base url
$config['base_url'] = 'http://localhost/yourproject/';
$config['index_page'] = '';
Welcome is the controller name as you said and aboutus would be a function name
Follow the PHP Codeigniter Naming Style Guide
Welcome.php
<?php
class Welcome extends CI_Controller {
public function index() {
}
public function aboutus() {
}
}
You may need a suitable .htaccess file to remove it so can work with out index.php
application
system
.htaccess
index.php
Try with <?php echo base_url ('index.php/welcome/aboutus');?>
In CodeIgniter, it dose not work like that.
<?php base_url ('welcome/aboutus') ?>
What it means is that Welcome is the name of the controller and aboutus is the name of the function in the controller.
So when you click on the link generated by the above code, it will search for a function named aboutus in the controller file Welcome.
To load about us using your link, the controller should look like this,
<?php
class Welcome extends CI_Controller {
public function index() {}
public function aboutus() {
$this->load->view('Name of the view file');
}
} ?>
I think you should know What is MVC Framework? and How Dose it Work?.
Have a look at these::
CI MVC Framework
CI MVC Static Page

Site URL CodeIgniter Not Working

I have Controller on CI like this
class Testing extends CI_Controller {
//put your code here
public function xx() {
$this->load->helper('url');
$this->load->view('testing');
}
public function linkURL() {
$this->load->helper('url');
$data['test'] = "testing123";
$this->load->view('xxx_view', $data);
}
}
I'm running on function linkURL and call view xxx_view, the code on view like this
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$segment = array('Testing', 'xx');
?>
Link
</body>
view call a href and using helper site_url to call Controller Testing and function xx. But the link is not working. I am already capture on firebug and link looks like weird. The Link on href contain *http://::1*. How to solved that link
You can print_r($_SERVER) in your controller and check it. or You can use
$config['base_url'] = 'http://localhost:8081/your-project/'
In my opinion, seem to you sent data test to view xxx_view but not using this variable. try echo $test and I see in url using xxx not xx
You need to autoload URL helper in your config/autoload.php.This time you are loading URl helper in function. That will not work for your view.

Laravel 5 How to dynamically include External CSS and JS file depends on controllers/routes

I'm wondering what could be a good way to
dynamically include a js file in my main blade
template like this
<?php
$currentRoute = Route::currentRouteName();
$currentRoute = $currentRoute?$currentRoute:'home';
$currentRoute = explode('.',$currentRoute);
?>
<script src="{{$currentRoute[0]}}.js"></script>
but just a less tricky :)
I did something like that in that past.
Here, is my "trick" to do this.
First you need a top level controller class
class MainController extends Controller {
protected static $js_files = [];
protected static $css_files = [];
public static function boot()
{
view()->share('js_files', static::$js_files);
view()->share('css_files', static::$css_files);
  parent::boot();
}
}
Then, all of your controllers must inherit from your MainController class.
The reason I use the keyword "static::" instead of "self::" is because I want to retrieve the latest child properties and not the "MainController" property, which would gave me the keywords "self::".
See http://php.net/manual/en/language.oop5.late-static-bindings.php
In your blade layout, in the head section do the following :
#foreach($css_files as $src)
<link href="{{$src}}" rel="stylesheet">
#endforeach
and before your closing body tag
#foreach($js_files as $src)
<script src="{{$src}}"></script>
#endforeach
In each controllers, you have to put in the $js_files and $css_files inherited properties the sources of all of your js/css.
if you want to include a JS/CSS only for one route, you can add the source of the file directly in the method.
And voilà !

CodeIgniter's url segmentation not working with my JSON

It's my first post in here and I haven't yet figured out to format my post properly yet, but here it goes.
So basically I can only get my code to work if i point directly to a php-file. If I try to call a method within my controller, nothing seems to happen.
My JavaScript:
$(document).ready(function() {
$(".guide_button").click(function(){
var id = $(this).text();
var data = {};
data.id = id;
$.getJSON("/guides/hehelol", data, function(response){
$('#test').text(response.id);
});
return false;
});
});
My markup:
<div id="content_pane">
<ul>
<li>RL</li>
<li>LG</li>
<li>RG</li>
<li>SG</li>
<li>GL</li>
<li>MG</li>
</ul>
</div>
<div class="description">
<h3>Description</h3>
<p id="test">This text area will contain a bit of text about the content on this section</p>
</div>
My Controller:
<?php
class Guides extends CI_Controller {
public function Guides()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
}
public function index()
{
$this->load->view('guides_view');
$title = 'Some title';
}
public function hehelol() //The controller I am desperatly trying to call
{
$id = $_GET['id'];
$arr = array ('id'=>$id);
echo json_encode($arr);
}
}
It might be my controller I have done something wrong with. As it is the code only works if create a hehelol.php file and refer to it directly like this.
$.getJSON("hehelol.php", data, function(response){
$('#test').text(response.id);
});
Anyone who knows what I need to do to make my controller work properly? Help please! :)
i just put your exact code in its entirety in my codeigniter app and it worked for me. Meaning I used this: ...$.getJSON("/guides/hehelol",...
Because you are making a $_GET request, you have to enable query strings.
In your config.php file, make sure this line is set to TRUE:
$config['allow_get_array']= TRUE;

How to display view without template?

I have view (frontend) in my own component (view.html.php):
class MevViewMev extends JView{
function display($tpl = null){
parent::display($tpl);
}
}
And template:
<?php defined('_JEXEC') or die('Restricted access'); ?>
<div>
ASFADSFDSF
</div>
How to display it without joomla template (head section, styles, etc). I want to call this part of jquery onclick method in the window.
To display the component only add "tmpl=component" parameter to url.
If need to display something besides component's view it can be customized - create "component.php" file in template's root folder and include in it whatever you need.
More templates can be done in the same way - create "some_template.php" in template's root folder and add "tmpl=some_template" parameter to url.
Start Edit
OK so the below works, but I found a better way. In your controller do ...
if (JRequest::getVar('format') != 'raw') {
$url = JURI::current() . '?' . $_SERVER['QUERY_STRING'] . '&format=raw';
header('Location: ' . $url);
// or, if you want Content-type of text/html just use ...
// redirect($url);
}
End Edit
You can set 'tmpl' to 'component', as suggested by Babur Usenakunov, in which case scripts and css may be loaded, like ...
JRequest::setVar('tmpl','component');
However if you want to create raw output you can add &format=raw or in your component make a view of type 'raw' ...
Unfortunately the only functional way I can find to make a viewType of raw render correctly is to call exit() after the view class calls parent::display() ...
In your controller.php ...
class com_whateverController() extends JController
{
function __construct()
{
// the following is not required if you call exit() in your view class (see below) ...
JRequest::setVar('format','raw');
JFactory::$document = null;
JFactory::getDocument();
// or
//JFactory::$document = JDocument::getInstance('raw');
parent::__construct();
}
function display()
{
$view = $this->getView('whatever', 'raw');
$view->display();
}
}
then in views/whatever/view.raw.php ...
class com_whateverViewWhatever extends JView
{
public function display($tpl = null)
{
parent::display();
exit; // <- if you dont have this then the output is captured in and output buffer and then lost in the rendering
}
}
I know this comes in very late, but for future readers, here's how I did it for my extension, without editing the template, or adding anything in the URL (since I have control over neither of those):
jimport('joomla.application.component.view');
use \Joomla\CMS\Factory;
// Comp stands for the Component's name and NoTmpl stands for the View's name.
class CompViewNoTmpl extends \Joomla\CMS\MVC\View\HtmlView {
// Force this view to be component-only
public function __construct() {
$app = Factory::getApplication();
$app->input->set('tmpl', 'component');
parent::__construct();
}

Resources