Welcome Guest, Not a member yet? Register   Sign In
Use function instead of a controller
#1

[eluser]ruiganga[/eluser]
Hi

This is my first CI project so please forgive my dumbness.

After viewing 3 full tutorials, I've designed my app like this:

Main Controller
- index function show app home
- login function show form
- logout function kills session and redirect to app root

Looks simple. I can do it if Login and Logout are Controllers, but instead, I wish them to be functions of Main controller.

site.com -> execute Main Controller index function

I expect to

site.com/login -> execute Main Controller login function

Any way to do this?

thanks for any help
#2

[eluser]CroNiX[/eluser]
Have a look at routes in the user guide.
#3

[eluser]ruiganga[/eluser]
Oh yeah. very simple indeed after knowing what routes are for.

The only code I needed was this:

Code:
$route['logout'] = "main/logout";

Thanks.

But now I want that site.com/main/logout to not exist, or in other words, at least redirect to a new url.

I've couldn't accomplish this with routes. Should I use the controller instead? how?

Thanks for any help.
#4

[eluser]ruiganga[/eluser]
Routing is cool Tongue

Here is the code I use to route any time main is in the url

Code:
$route['main/(:any)'] = "main/redirect";

Now in the Redirect FUNCTION I need to catch the any part to redirect to the correct page.

Any tip for me?
#5

[eluser]ruiganga[/eluser]
I got the solution as:

Code:
// first in the route I've written
$route['main'] = "main/redirect";
$route['main/(:any)'] = "main/redirect";


//and then in the Main CONTROLLER at Redirect FUNCTION
public function redirect(){
  $goToLink = "http://".base_url()."".$this->uri->segment(2);
  redirect($goToLink);
}

If anyone has a tip to do it more intelligently I appreciate.

I'm starting loving this Tongue

Thanks to CroNiX for pointing me the routes Smile
#6

[eluser]CroNiX[/eluser]
One thing with routes...you need to have the order go from the uri with the most segments, to least.
Code:
$route['main/(:any)'] = "main/redirect"; //two segments, main + (:any)
$route['main'] = "main/redirect"; //one segment, main
In routes, you can also pass in any captured segments to the controller/method.
Code:
$route['main/(:any)'] = "main/redirect/$1"; //$1 will be (:any)
Then in main{}...
Code:
function redirect($url = null) //$url is (:any) from the route
{
  if ( ! is_null($url))
  {
    redirect($url);
  }
}
You also shouldn't have to have http:// AND base_url(). base_url() contains the http://, or at least it should in the config. Also, when using redirect(), CI adds the base_url() automatically so all you need to do is pass it the segments
Code:
redirect($this->uri->segment(2));  //redirects to base_url() + segments
#7

[eluser]t'mo[/eluser]
[quote author="ruiganga" date="1390750844"]...
If anyone has a tip to do it more intelligently I appreciate.

I'm starting loving this Tongue

Thanks to CroNiX for pointing me the routes Smile[/quote]

Check out CodeIgniter's "remap" function here




Theme © iAndrew 2016 - Forum software by © MyBB