I just started using Kohana and I'm having some trouble rendering a simple view. I created the following controller:
class Controller_Tracer extends Controller {
public function action_index() {
$this->request->response = View::factory('tracer');
}
}
Then I've created this view in application/views/tracer.php:
Tracer view
Now when I try to access the controller via http://mydomain/index.php/tracer/index it's just displaying a blank page. It seems to be finding both the controller and view since when I change the names there are errors but it's just not displaying anything.
Does anybody know what could be the issue?
In Kohana 3.1 you would instead use:
$this->response->body(View::factory('tracer'));
Have a quick look over the docs for migrating from 3.0 to 3.1.
Besides davgothic's solution, you also can use Controller_Template. Using Controller_Template make it easier to manage template & content
class Controller_Tracer extends Controller {
public $template = 'yourtemplatefile'; // HTML template inside views folder
public function before() {
parent::before();
$this->template->title = 'My Website';
}
public function action_index() {
$this->template->content = 'Hello World';
}
public function action_trace() {
$this->template->content = View::factory('tracer');
}
}
Inside views/yourtemplatefile.php:
<html>
<head>
<title><?php echo isset($title) ? $title : ''; ?></title>
</head>
<body>
<h1><?php echo isset($title) ? $title : ''; ?></h1>
<?php echo isset($content) ? $content : ''; ?>
</body>
</html>
Inside views/tracer.php:
<p>This is tracer.</p>
<p>Nulla vitae elit libero, a pharetra augue.</p>
If you try to access http://mydomain/index.php/tracer/index, you will get:
My Website
Hello World
If you try to access http://mydomain/index.php/tracer/trace, you will get:
My Website
This is tracer.
Nulla vitae elit libero, a pharetra augue.
Hope that helps!
Related
I have two controllers, two views and a masterpage:
dashboard controller:
class Dashboard extends CI_Controller {
public function index()
{
if($this->session->userdata('login') == true){
$data['title'] = 'Dashboard';
$data['content'] = 'pages/dashboard';
$this->load->view('layout/master', $data);
}
else{
redirect('auth');
}
}
customers controller:
class Customers extends CI_Controller {
public function index()
{
$data['content'] = 'pages/add_customer';
$this->load->view('layout/master', $data);
}
public function add(){
$data['content'] = 'pages/add_customer';
$this->load->view('layout/master', $data);
}
}
And my master page:
<head>
<meta charset="utf-8" />
<title><?php echo isset($title)? $title: NULL; ?></title>
<?php $this->load->view('layout/header'); ?>
</head>
<body>
<!-- BEGIN PAGE BASE CONTENT -->
<?php $this->load->view($content);?>
<!-- END PAGE BASE CONTENT -->
</body>
The problem is When i call dashboard, the view runs in the template and everything is fine. When i call customers, again everything runs fine and the template is OK. But when i call the add method from customers controller, It's like the master page doesn't work and the template messed up. Like there is no CSS or something.
What's the problem?
Thanks in advance :)
I personally do not rely on native CI functionality for any template/view stuff. I use Stencil, which isn't actively developed anymore, but it worked on 2 and works on 3. I modified the core library file to handle session and config variables, but this thing is beautiful. It's at the center of every one of my CI deployments.
Stencil on scotch.io via Github
This absolutely doesn't answer your question and certainly deserves to be down-voted or flagged for that reason. But CI view handling is awkward, so I never endorse using it. I don't know why Stencil isn't built in.
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.
Hello everyone,
I am a newbie to Magento. I want to learn **ajax process in Magento.** Can anyone help me to understand ajax in Magento with one simple example?
Your help will be highly appreciated.
I give you a simple example for you. To work with basic jQuery Ajax in Magento you have work in phtml page and Controller.
Just add the script in phtml page:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".like-result").click(function() {
//alert(this.id);
var id = this.id;
//alert(custid);
jQuery(".notify-status").hide();
jQuery(".notify-loader").show();
jQuery.ajax({
type: "POST",
data: 'pid=' + id,
url:'http://192.168.2.3/subhranil-demo/blog/index/likecount',
success:function(response){
if (response) {
jQuery(".notify-loader").hide();
jQuery(".notify-status").show();
jQuery("#un"+id).html(response);
}
}
});
});
});
</script>
In the above script under jQuery.ajax you can also see type, data, url. type is used for sending process like POST or GET; in data, you will send information to the controller; in URL, you can declare the controller path. Here I have a 'blog' module and I write the public function under 'index' controller and I give the function name 'likecount'. Also here my base path is http://192.168.2.3/subhranil-demo/. So I add the link to URL as following structure: http://192.168.2.3/subhranil-demo/blog/index/likecount.
Now I go to 'IndexController.php' in my controller's folder of blog module and open it. Under the class I add the following function:
public function likecountAction()
{
$blogload = Mage::getModel('blog/blog')->load($_POST['pid']);
$newid = $blogload['like']+1;
$data = array('like'=> $newid);
$blogload->addData($data);
try {
$blogload->setId($_POST['pid'])->save();
echo $newid;
} catch (Exception $e){
echo $e->getMessage();
}
}
Here in the Blog Database, I have the fields like pid (as a primary key) and like. the function works like that when you click on 'like-result' class the like increase +1.
My div structure also like that:
<?php
$allCollection=Mage::getModel("blog/blog")->getCollection();
$allCollection->addFieldToFilter('status',1);
if ($allCollection->count() >= 1)
{
$news = array();
?>
<div class="blog clearfix">
<?php
foreach ($allCollection as $news)
{?>
<p class="like-result" id="<?php echo $news->getId(); ?>"> <?php echo $news->getLike(); ?> </p>
<a style="display: none;" class="notify-loader"><img src="http://www.sendhersomething.com/skin/frontend/megatron/default/images/ajax/notify-loader.gif"></a>
<a style="display: none;" class="notify-status"><img src="http://www.sendhersomething.com/skin/frontend/megatron/default/images/ajax/ststus.png"></a>
<?php } ?>
</div>
<?php } ?>
Try this!
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" />
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;