CodeIgniter Forums
Can't call the right action - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Can't call the right action (/thread-12236.html)

Pages: 1 2


Can't call the right action - El Forum - 10-11-2008

[eluser]Plume[/eluser]
Hi people !

It was a long time since the last.

I organize my controllers into sub folders. I have a folder named mail/ containing a Read.php file. This last one is called thanks to the URL http://plume-conception.ath.cx/exotech/mail/read/2 where the last parameter is the id of the mail I would see ( if everything works... )

This file contains this class :
Code:
<?php
class Read extends Controller{

  /**
   * @access private
   * @var string the name of the mail table
   */
  private $tableMail = 'mail';

  /**
   * @access private
   * @var string name of table player
   */
  private $tablePlayers = 'players';

  /**
   * @access private
   * @var array user sess data
   */
  private $userSess = array();

  /**
   * @access private
   * @var array player sess data
   */
  private $playerSess = array();

  /**
   * @access private
   * @var int the mail id
   */
  private $idMail = 0;


  /**
   * This construct check if the user is logged
   * If not, user is redirected at home
   * Else, we initialize userSess & playerSess
   * And we load the right language file
   *
   * @access public
   * @param int $idMail mail id
   */
  public function __construct($idMail){

    parent::Controller();
    
    $this->idMail = $idMail;

    if(!$this->session->userdata('logged_in')){

      redirect('home/home');
    }

    $this->userSess = $this->session->userdata('user');
    $this->playerSess = $this->session->userdata('player');

    $this->lang->load('mail', $this->userSess['localisation']);
  }

  /**
   * This function display the mail we want to read
   *
   * @access public
   */
  public function index(){

    echo $this->idMail;
  }
}

But when I call this controller thanks to the URL quoted upper, I get the error messages :
Quote:A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Read::__construct(), called in /Applications/xampp/xamppfiles/htdocs/exotech/system/codeigniter/CodeIgniter.php on line 198 and defined

Filename: mail/Read.php

Line Number: 43
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: idMail

Filename: mail/Read.php

Line Number: 47

Someone could explain to me what's wrong ?

And I recall that I'm still french, so beg the bad english. Please ! And don't hit me :/

@tchaOo┬░


Can't call the right action - El Forum - 10-11-2008

[eluser]Mirage[/eluser]
Given your URL example the url parameter would be passed to the index function, not the constructor. So

Code:
public function __construct() {
....
}

public function index($idMail) {
      echo $idMail;
}

That said, if you like receiving uri segments in your controller actions, I'd always make them optional to avoid nasty error. Imagine someone would type the url with out the message id segment. Your index would be called, but you'd get the same errors you just described. If you check parameter availability in the action, you can more graceful exit on error. So

Code:
public function index ($idMail=null) {
    if (is_null($idMail) {
        show_404()
    }

    echo $idMail;

}

Cheers,
-m


Can't call the right action - El Forum - 10-11-2008

[eluser]Plume[/eluser]
Thanks for answer :]

I already made like you said but I got :
Quote:404 Page Not Found

The page you requested was not found.

I really don't know wot's wrong :/

@tchaOo┬░


Can't call the right action - El Forum - 10-11-2008

[eluser]Mirage[/eluser]
How are you accounting for the 'exotech' segment? is your index.php in an 'exotech' folder of your webroot? Or do you have routes that map it to the proper destination?

-m


Can't call the right action - El Forum - 10-11-2008

[eluser]Plume[/eluser]
Have this organisation :
Code:
xampp/htdocs/
    exotech/
        index.php
        system/

:]


Can't call the right action - El Forum - 10-11-2008

[eluser]Mirage[/eluser]
Looks ok and should work unless you changed something else...

Enable debugging and see what the logs are saying. Also, you can put some simple echo statements in your controller file (e.g. before the class definition and in the constructor) to make sure CI is finding the controller properly.

Again show me the url you are requesting now and what your index function looks like.

Cheers,
-m


Can't call the right action - El Forum - 10-11-2008

[eluser]Plume[/eluser]
Ok, I made everything of this :]

This is what you want :
; URL : http://plume-conception.ath.cx/exotech/mail/read/2
; logs :
Code:
DEBUG - 2008-10-11 22:47:46 --> Config Class Initialized
DEBUG - 2008-10-11 22:47:46 --> Hooks Class Initialized
DEBUG - 2008-10-11 22:47:46 --> URI Class Initialized
DEBUG - 2008-10-11 22:47:46 --> Router Class Initialized
DEBUG - 2008-10-11 22:47:46 --> Output Class Initialized
DEBUG - 2008-10-11 22:47:46 --> Input Class Initialized
DEBUG - 2008-10-11 22:47:46 --> Global POST and COOKIE data sanitized
DEBUG - 2008-10-11 22:47:46 --> Language Class Initialized
DEBUG - 2008-10-11 22:47:46 --> Loader Class Initialized
DEBUG - 2008-10-11 22:47:46 --> Helpers loaded: form, language, url
DEBUG - 2008-10-11 22:47:46 --> Session Class Initialized
DEBUG - 2008-10-11 22:47:46 --> Validation Class Initialized
DEBUG - 2008-10-11 22:47:46 --> Controller Class Initialized
DEBUG - 2008-10-11 22:47:46 --> Language file loaded: language/french/mail_lang.php
ERROR - 2008-10-11 22:47:46 --> 404 Page Not Found --> read/2
; index action
Code:
public function index($idMail = null){

    if(is_null($idMail)){

      show_404();
    }

    echo $idMail;
  }

And no need to do some echo, I can assure you that he find the right file, initialisation is OK as you can see thanks to the log ;]

If I'm here, it's really because I'm lost. But I always could do stupidity for sure ^^'

Do you need anything else ?

For informations, everything else I already did works well.


Can't call the right action - El Forum - 10-11-2008

[eluser]Mirage[/eluser]
Ok, found the problem... duh!

You are calling the read controller in the mail directory. BUT your first parameter - which defines the action - is '2'. You don't have a method called '2'. Therefore it fails.

If you change your url to: http://plume-conception.ath.cx/exotech/mail/read/index/2 then it will work.

Your use of controllers/actions is a bit unusual. You would perhaps be better of using a 'mail' controller, with a 'read' action. If you must make your own controller I suggest one of the following:

1. Add a route to your routes.php file:
Code:
$route['.*mail/read(.*)']='mail/read/index$1';
or

2. Add a _remap method to your controller and evaluate things in there, finally mapping to the proper function.

Not knowing your specific goals, I'd highly recommend creating a mail controller with a read action rather than creating separate controllers for each action.

Let me know how it goes....

Cheers,
-m


Can't call the right action - El Forum - 10-11-2008

[eluser]Plume[/eluser]
Sorry but I understood in reading doc that using subfolder to organize Controllers its like :
A folder = Controller
A file in this folder = Action
So mail/read

Yes, I saw that mail/read/index/2 works but usually I just have to do e.g : mail/view ( for viewing list ) :
Code:
<?php
class View extends Controller{

  private $tableUsers = 'users';
  private $tablePlayers = 'players';
  private $tableMail = 'mail';

  private $userSess = array();
  private $playerSess = array();

  public function __construct(){

    parent::Controller();
    if(!$this->session->userdata('logged_in')){

      redirect('home/home');
    }

    $this->userSess = $this->session->userdata('user');
    $this->playerSess = $this->session->userdata('player');
    $this->lang->load('mail', $this->userSess['localisation']);
  }

  public function index(){

    $this->load->database();

    $selectMail = 'SELECT '.$this->tableMail.'.id AS mail_id, subject, date_send, is_read, name, firstname ' .
        'FROM '.$this->tableMail.' ' .
          'JOIN '.$this->tablePlayers.' ' .
          'ON from_id = '.$this->tablePlayers.'.id ' .
        'WHERE to_id = '.$this->playerSess['id'].';';
    $selectMailStmt = $this->db->query($selectMail);
    $this->selectMailArray = $selectMailStmt->result_array();

    $this->layout->view('mail/view');
  }
}

Am I so wrong ? :/


Can't call the right action - El Forum - 10-11-2008

[eluser]Mirage[/eluser]
Quote:Sorry but I understood in reading doc that using subfolder to organize Controllers its like :
A folder = Controller
A file in this folder = Action

That's a misunderstanding. If you organize controllers into folders, you're just adding a grouping segment for your controllers. Actions are still to be defined within each controller.

Lets assume for example you have and 'admin' section on your application. In the admin you can operate on things like users, forums, blogs, etc, etc.

In that case you may not want an admin controller. But rather an admin group (aka subfolder) with controllers for users, forums and blogs. Each of those controllers then providing the appropriate actions.

HTH,
-m