Welcome Guest, Not a member yet? Register   Sign In
My view appears twice
#11

[eluser]xwero[/eluser]
i just did a quick test with following file structure
- controllers
-- index.php
-- home
--- index.php
The index.php has this code
Code:
<?php
class Index extends controller
{
    function index()
    {
        echo 'hey';
    }
}
The urls index and home/index worked.
Then i added in the controllers directory the home.php file and i got routed to that controller so files are higher class than directories.

Plume what is in your routes.php file?
#12

[eluser]Plume[/eluser]
Not sure to understand everything cause bad english.

Have I to route every controller located in sub folder ? What's your content of home.php ?

I tested :
Code:
<?php
class Index extends controller
{
    function index()
    {
        echo 'hey';
    }
}
And I'm obtaining :
Quote:heyhey

When I test :
Code:
<?php
class Index extends Controller{
    
    function index(){
      
      parent::Controller();
      $this->layout->view('home/index');
      echo 'bouh';
    }
}
I'm obtaining :
Code:
bouhbouh
// My view x2

And when I test another url like home/test :
Code:
<?php
class Test extends Controller{
  
  public function test(){
    
    echo 'Hey';
  }
}
I'm obtaining :
Quote:Hey

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Applications/xampp/xamppfiles/htdocs/exotech/system/application/controllers/home/test.php:6)

Filename: errors/error_404.php

Line Number: 1
404 Page Not Found

The page you requested was not found.

I'm lost :/

Layout.php :
Code:
<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Layout{

  var $obj;
  var $layout;

  function Layout($layout = "layout"){
    
    $this->obj =& get_instance();
    $this->layout = $layout;
  }

  function setLayout($layout){
    
    $this->layout = $layout;
  }

  function view($view, $data=null, $return=false){
    
    $loadedData = array();
    $loadedData['content_for_layout'] = $this->obj->load->view($view,$data,true);

    if($return){
      
      $output = $this->obj->load->view($this->layout, $loadedData, true);
      return $output;
    }else{
      
      $this->obj->load->view($this->layout, $loadedData, false);
    }
  }
}

layout.php ( main layout ) :
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;link rel="stylesheet" type="text/css" href="main.css" /&gt;
  
  &lt;title&gt;Exotech MS&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  <h1>Exotech MS</h1>
  <ul>
    <li><a href="&lt;?php echo ('profile/inscription'); ?&gt;">Inscription</a></li>
  </ul>
  
  &lt;?php echo $content_for_layout; ?&gt;
  
  <div>
     Codé par <a href="mailto:[email protected]">Plume</a>
  </div>
&lt;/body&gt;
&lt;/html&gt;
#13

[eluser]xwero[/eluser]
The home.php content was
Code:
&lt;?php
class Home extends Controller{
  
  public function index(){
    
    echo 'Hey home';
  }
}

You dont have to route every controller but i was trying to mimic the scenario you describe here and my output was the one i expected to see. So i asked myself why do you get the output twice and that is why i asked for the content of your routes.php file. Maybe that is the cause of your problem.
#14

[eluser]Plume[/eluser]
Just having this :
Code:
$route['default_controller'] = "index";
$route['scaffolding_trigger'] = "";

Don't think there is a problem but ... ^^
#15

[eluser]Plume[/eluser]
Ok, I changed my test.php file :
Code:
&lt;?php
class Test extends Controller{
  
  public function index(){
    
    echo 'Hey';
  }
}
And I just get well "Hey", not "HeyHey".

I'm now trying to find why Index controller doesn't work and just this one :/
#16

[eluser]Dready[/eluser]
As wiredesignz was telling you, you have double output because the "index" method is called twice : once when the object is created, and once when code igniter, through the routing process, call the default method. To be a little more explicit :

Code:
class testme {

function testme() {
   echo "hi there !";
}

}

$foo = new testme;

This code will echo "hi there !";

And now if you try :

Code:
class testme {

function testme() {
   echo "hi there !";
}

}

$foo = new testme;
$foo->testme();
This code will echo "hi there !hi there !";

When you have a controller with "index" as the class name , and you hit the URL http://youserver/index.php/index , codeigniter first create an object of your controller ( new Index() ) and because the default method called by codeigniter is "index", and because in your URL you didn't specify another method, codeigniter then call your "index" method once again.
Now if you create another method inside your controller, for example :
Code:
class Index extends controller {
  function index() {
    echo "in index ";
  }
  function foo () {
    echo "in foo ";
  }
}

and then you hit the URL http://youserver/index.php/index/foo it will echo "in index in foo ". To resume if I were you I wouldn't make a controller having "index" a the class name.
Good luck.
#17

[eluser]Plume[/eluser]
Ok, I understand. Sorry :/

But, if I can't create a controller named Index, how can I affect a default controller when my routing file specify index has default ?

Just change default will be OK ?
Have I to be careful every time to have no method named like my Controller ? Is that it or I'm always wrong ?
#18

[eluser]Dready[/eluser]
[quote author="Plume" date="1222804240"]Ok, I understand. Sorry :/

But, if I can't create a controller named Index, how can I affect a default controller when my routing file specify index has default ?

Just change default will be OK ?
Have I to be careful every time to have no method named like my Controller ? Is that it or I'm always wrong ?[/quote]

Yes just change the default. Yes be careful every time to have no method named like your Controller. :-)

You got it Plume... and welcome to CI !
#19

[eluser]Plume[/eluser]
Yeehaaa ! So thanks boys !

I now can continue further !

See ya soon for next problem :d

@tchaOo°




Theme © iAndrew 2016 - Forum software by © MyBB