Site URL CodeIgniter Not Working - codeigniter

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.

Related

Codeigniter's controller data always shows on top of the html

I am trying to get the data of another function inside the controller, but there is a problem when I load it with the view. Anyway I placed the output of the controller inside the view and it still goes to the top of the HTML
this is the controller code
public function index()
{
$data['record'] = $this->samples();
$this->load->view('home',$data);
}//end index()
public function samples(){
echo "string";
}
this is the view
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php echo $record;?>
<h1>This is a sample</h1>
</body>
</html>
Now when I go to the browser it looks ok, but when you go to the source code you can see the data is on top of HTML. Why is that?
source code from view
You should return it
public function index()
{
$data['record'] = $this->samples();
$this->load->view('home',$data);
}
public function samples(){
return "string";
}

Use master template in codeigniter

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.

Laravel 4 content yielded before layout

I am using a fresh build today of Laravel 4.
I have a dashboardController
class DashboardController extends BaseController {
protected $layout = 'layouts.dashboard';
public function index()
{
$this->layout->content = View::make('dashboard.default');
}
}
I have a simple route
Route::get('/', 'DashboardController#index');
I have a blade layout in views/layouts/dashboard.blade.php
For the sake of saving everyone from all of the actual HTML ill use a mock up.
<html>
<head>
<title></title>
</head>
<body>
#yield('content')
</body>
</html>
I have a default blade file in views/dashboard/ that has the following (edited for simplicity)
#section('content')
<p>This is not rocket science</p>
#stop
For some reason the content gets generated before the layout.
I am using a different approach to set the layouts globally to routes using a custom filter. Put the following filter into the app/filters.php
Route::filter('theme', function($route, $request, $response, $layout='layouts.default')
{
// Redirects have no content and errors should handle their own layout.
if ($response->getStatusCode() > 300) return;
//get original view object
$view = $response->getOriginalContent();
//we will render the view nested to the layout
$content = View::make($layout)->nest('_content',$view->getName(), $view->getData())->render();
$response->setContent($content);
});
and now instead of setting layout property in the controller class, you can group the routes and apply the filter as shown below.
Route::group(array('after' => 'theme:layouts.dashboard'), function()
{
Route::get('/admin', 'DashboardController#getIndex');
Route::get('/admin/dashboard', function(){ return View::make('dashboard.default'); });
});
When creating the views, make sure to use the #section('sectionName') in all the sub views and use #yield('sectionName') in the layout views.
I find it easier to do my layout like this for example. I would create my master blade file like so
<html>
<body>
#yield('content');
</body>
</html
And in the blade files that I want to use the master at the top i would put
#extends('master')
then content like so
#section('content')
// content
#stop
Hope this helps.
When you use controller layouts, i.e. $this->layout->..., then you get access to data as variables, not sections. So to access content in your layout you should use...
<html>
<head>
<title></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
And in your partial, you would not use #section or #stop...
<p>This is not rocket science</p>

Redirecting to another page. How do I display a countdown?

I'm just wondering if the following is possible:
I don't know anything about JQuery and I don't know how to make my own javascripts yet.
When a user logs into my website, they'll get a page that says "login completed".
Under that sentence, I would like to get a sentence that counts down the seconds until they are redirected. Is there a simple way to do this?
I already have a countdown function:
When the function is first called, the view 'view_login_success' is loaded and the $data[] array is passed to it. $data['sec'] holds the seconds remaining that should be printed on the screen.
When the do-while loop is over, the user should be redirected to the homepage.
What happends is, the 'view_login_success'-view isn't loaded at all and after 5 seconds, the user is redirected to the homepage.
function timerIn() {
$now = time();
$this->load->view('view_login_success', $this->data);
do {
if (time() - $now != 0) {
$this->data['sec'] = $this->data['sec'] - 1;
$this->load->view('view_login_success', $this->data);
$now = time();
}
} while ($this->data['sec'] != 1);
$this->data['sec'] = 5;
redirect('user/start');
}
Here is the view:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login geslaagd</title>
<?php $this->load->view('templates/header2'); ?>
</head>
<body>
<h2 style="text-align: center;"><?php echo strtoupper(substr($this->session->userdata('gebruikersnaam'),0,1));
echo substr($this->session->userdata('gebruikersnaam'),1); ?>, U bent ingelogd!</h2>
<p>U word doorverwezen: <?php echo $sec; ?> seconden...</p>
</body>
<?php $this->load->view('templates/footer'); ?>
If there's a way to do this in JQuery of Javascript, then please explain to me what I should do to get it to work.
Thanks in advance! :)
That's not how php works.
If you want the page to redirect after 5 secs you have to do so in the JS code.
You redirect the user without sending the view to the client.
Right now you are only waiting 5 seconds to redirect. So:
Show the view
The JS will have the timer and then redirects.
The javascript code would be something like this:
setTimeout(function(){
window.location.href = 'theURLyouwant';
},5000)
OR you could use the ol' meta tag
<meta http-equiv="refresh" content="5; URL=theURLyouwant">
Use code-igniter to solve this problem. Use this reference class output. Output class will be loaded automatically. You don't need to load this. Use this function to set header.
$this->output->set_header('refresh:time-to-redirect; url='.site_url("your-url"));
For more detail about this class.

Codeigniter loading library

i have a controller like this one :
<?php if( ! defined('BASEPATH')) exit ('No direct script acces allowed');
class Halaman extends CI_controller{
function __controller(){
parent::controller;
$this->load->helper(array('url','form'));
$this->load->library('table');
}
function index(){
$this->load->library(array('form_validation'));
$this->load->view('view_halaman');
}
function daftar(){
$this->load->model(url);
}
}
and i have a views like this one
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pembagian menggunakan validasi!!</title>
</head>
<body>
<h1>Daftar Ulang</h1>
<?php echo form_open('halaman/daftar'); ?>
<?php $data=array(
array('Field','isi data'),
array('nama',form_input('user','tulis username')),
array('password',form_password('pass','password')),
array('email',form_input('email','tulis email di sini'))
);
echo $this->table->generate($data);
?>
<?php echo form_close(); ?>
<p><br/>Page rendered in {elapsed_time} seconds</p>
</html>
enter code here
what i get is this one :
where is my mistake ? im sorry i am really newbie in codeigniter. thanks a lot .
$table is not a member of the view object. So you can't call it from the view template. You need to move the echo $this->table->generate($data); to the controller, and either assign it to a view variable or just echo it from the controller.
I think the problem is that the library wasn't loaded (that's the error in the red lined box). This happends when you don't follow the CI library name conventions. The Table library should be in application/libraries/Table.php and class must be someting like:
class Table {
// Your code
}
http://codeigniter.com/user_guide/general/creating_libraries.html
Also when you load the library, use the name with capital T:
$this->load->library('Table');
Regards,

Resources