Welcome Guest, Not a member yet? Register   Sign In
Controllers & Parameters altogether
#1

[eluser]KillerQueen[/eluser]
I have been trying to use one controller in order to have multiple functions, but also receive some parameters. The code is similar to this one:

Code:
<?php
class Session_Start extends Controller {

function Session_Start()
{
    parent::Controller();
    // Loads Helpers
}

function index($msgNumber = null)
{
// Controller's Code
}

function login()
{
// Controller's Code
}
?>

So,
http://example.com/session_start/1 has to be interpreted like a parameter.
http://example.com/session_start/login has to be interpreted like a function.

I've searched in the archive, but it's not clear to me which is the best way to solve this. Routes? Have you got any examples?

Thank you very much in advance.
Alan.
#2

[eluser]sooner[/eluser]
i think it should be http://example.com/session_start/index/1 and the other is fine
#3

[eluser]KillerQueen[/eluser]
[quote author="sooner" date="1294175405"]i think it should be http://example.com/session_start/index/1 and the other is fine[/quote]

Yes, it's the common way, but I want to create short URLs and avoid "index" terms, in order to hide system and platform information.
#4

[eluser]Cristian Gilè[/eluser]
Hi KillerQueen,

you can use the remapping function call (http://ellislab.com/codeigniter/user-gui...#remapping).

For example:

Code:
<?php
class Session_Start extends Controller
{

function Session_Start()
{
    parent::Controller();
    // Loads Helpers
}

function _remap($method)
{
    if ($method == '1')
    {
        $this->index($method);
    }
    elseif($method == 'login')
    {
        $this->login();
    }
}

function index($msgNumber = null)
{
  echo $msgNumber;
}

function login()
{
  echo 'login area';
}

}

The controller class in your sample code doesn't have the closed brace.

Cheers
#5

[eluser]ecsyle31[/eluser]
1 is a value and will change, I assume. So remapping will not work.

Set this up in your routes.

Code:
$route['sesssion_start/(:num)'] = "session_start/index/$1";
#6

[eluser]KillerQueen[/eluser]
Thanks for your answer, Cristian!
It's a nice way of solving the problem.

I found a similar solution, doing:

config/routes.php
Code:
$route['session_start/(:num)'] = "session_start/index/$1";

And the same code I posted before in controllers/ssession_start.php

However, your solution is elegant and you can forget about modifying the routes.php!
Thanks again for you reply!

Alan.
#7

[eluser]KillerQueen[/eluser]
[quote author="rlindauer" date="1294188491"]1 is a value and will change, I assume. So remapping will not work.[/quote]

Perhaps you could do something like this:
Code:
function _remap($method)
{
    if (is_int($method))
    {
        $this->index($method);
    }
    elseif($method == 'login')
    {
        $this->login();
    }
}
#8

[eluser]Cristian Gilè[/eluser]
To test if a variable is a number or a numeric string you must use is_numeric() not is_int()




Theme © iAndrew 2016 - Forum software by © MyBB