Welcome Guest, Not a member yet? Register   Sign In
default_controller & default index() method: Clarifications/tips please...
#1

[eluser]mhulse[/eluser]
Hi,

I am slowly learning CI...

Long story short, when I visit this uri:

http://mydomain.com/folder/(default_controller)

I want to load a default view (no probs here).

But when I want to pass arguments, I have to do something like this:

http://mydomain.com/folder/default_controller/method/id/id/id

Keep in mind here, I am a noob...

Question 01: How can I do something like this:

http://mydomain.com/folder/method/id/id/id

(if default_controller is default, why would I need to specify it in the uri?)

Also...

Question 02: Is it possible to change the default method name?

One idea:
Code:
// Controller with no index()...

public function _remap($method)
{
    
    if ($method == 'index')
    {
        $this->other();
    }
    
}

public function other($id = 'foo') //...

The above works when I visit (NOT tested using default_controller):

http://mydomain.com/folder/controller

But it doesn't work when I try this:

http://mydomain.com/folder/controller/other/id

Not sure where I am going with this... Anyone have any thoughts? Clarifications? Tips? Philosophies? Best practices?

Question 03: How do you normally handle your default controller?

For example, I think I will opt to have my default_controller:

http://mydomain.com/folder/(default_controller)

... redirect to that project's "primary", non-default, controller. Seems like doing a redirect would give me full control over what people see in the uri. I dunno why, but I am not a fan of having to put the default_controller name in the uri just to pass arguments. Sad

Hmmm, maybe I need to sleep on all of this and get a fresh start in the morning. Big Grin

Anyway, a billion thanks in advance for the help!

Cheers,
Micky
#2

[eluser]Jameson.[/eluser]
Let's not go into detail, but instead try to get a bigger picture.

You're writing you want to pass arguments to the default controller...

Stop.

Think.

What IS A "default controller"?

It's the controller fired up when a visitor comes to your site. The very first page. You don't know anything yet about this person, not his geolocation, not his preferences, nothing. Neither he did express the need to filter any data, view any other page, search for any stuff...

So WHY do you need to pass some data to your controller at this point? What data could you pass?
From where would all these IDs (and also a method) appear?
http://mydomain.com/folder/default_contr...d/id/id/id

They would not apepar, because there was no link with custom method and parameters to follow. A person just typed in a bare domain name, just domain.tld, with no controller/method indicated, no arguments. THIS is where you need default controller, only in case not one is explicitly set. In all other cases you can, you should specify controller and method.

Hope that helps Wink
#3

[eluser]mhulse[/eluser]
Hi Jameson! Many thanks for your quick reply and professional advice. I greatly appreciate the help and direction.

[quote author="Jameson." date="1261787640"]Hope that helps Wink[/quote]

Ahhh, very interesting!

Yes, it definitely helps.

Just thinking out loud here:

I have an install of CI here (for example):

[url="#"]http://ci.mydomain.com/[/url]

And the default_controller loads a default view like one would expect.

From there, I think I will have a simple list of links to different CI experiments.

So, in this case, one would click on a link to go to a sub-folder controller. For example:

[url="#"]http://ci.mydomain.com/controller/[/url]

I think where I got confused was where the documentation says this:

Quote:[url="http://ellislab.com/codeigniter/user-guide/general/controllers.html#subfolders"]Organizing Your Controllers into Sub-folders[/url]
Each of your sub-folders may contain a default controller which will be called if the URL contains only the sub-folder. Simply name your default controller as specified in your application/config/routes.php file

Part of me assumed that I could utilize the default controller in my controller sub-folder in order to shorten the uri up a bit. Smile

Well, you have definitely helped clear things up. Thank you so much!

I just have a few more questions:

01) Can the default "index()" method be renamed?

Here is a workaround I just discovered:
Code:
// ...
public function index()
{
    
    $this->go();
    
}

// ...
public function go($id1 = 990, $id2 = 10, $id3 = 100, $id4 = 2) // ...
{

Using the above code, this uri works:

[url="#"]http://ci.mydomain.com/folder/controller/[/url]

...and this one I would use to pass parameters:

[url="#"]http://ci.mydomain.com/folder/controller/go/1000/20/50/3[/url]

That seems reasonable to me. Smile

02) By default, the default_controller is called "welcome". Just out of curiosity, do you prefer to rename yours?

Thanks so much for the help! I really appreciate it.

Have a great holiday.
Cheers,
Micky
#4

[eluser]mhulse[/eluser]
This looks like a good [url="http://ellislab.com/forums/viewthread/135187/"]_remap()[/url] example.
#5

[eluser]Jameson.[/eluser]
[quote author="mhulse" date="1261792209"]
Can the default "index()" method be renamed?
[/quote]Not literally, no.

[quote author="mhulse" date="1261792209"]
Here is a workaround I just discovered:
Code:
// ...
public function index()
{
    $this->go();
}
[/quote]
I wouldn't call it "renaming", more of a redirect. You could also use routing for that:
Code:
$route['controller'] = "controller/go";
I prefer the former (your) variant; this way it's all in one place and in case you want to supply arguments with redirect, closer to get to in order to edit.

[quote author="mhulse" date="1261792209"]
By default, the default_controller is called "welcome". Just out of curiosity, do you prefer to rename yours?
[/quote]
I have just one that is called "default_controller" and via _remap it sorts out which one of modules (HMVC) to launch.
#6

[eluser]mhulse[/eluser]
Thanks Jameson! That really helps clear things up for me. Smile

I am going to do some more experimenting. CI has be really fun to work with, I just wish I were better at PHP5 OOP. Well, anyway, practice makes perfect.

Thanks for helping out a noob!

Have a great new year.

Cheers,
Micky
#7

[eluser]mhulse[/eluser]
I just realized that I should be using multiple application folders for my different projects, vs using sub-folders in my controllers and views folders.

This 'revelation' helps clear things up for me...

I think I will ditch subfolder usage and go with one default_controller per application. Big Grin
#8

[eluser]mhulse[/eluser]
[quote author="mhulse" date="1261926957"]I just realized that I should be using multiple application folders for my different projects, vs using sub-folders in my controllers and views folders.[/quote]

Well, not sure if this will help other noobs, but...

After a few hours of forum searching, this technique worked well for me:

[url="http://ellislab.com/forums/viewthread/47560/"]Help with Multiple Applications[/url]

Specifically post #7 and #8.

This technique looked interesting:

[url="http://ellislab.com/forums/viewthread/44568/"][Suggestion] Enable multiple applications to share the core CI files[/url]

But I opted for the first one because it seemed less complicated.

I really wish I could use the symlinks technique on my host. That seems like the best solution.

Cheers,
Micky




Theme © iAndrew 2016 - Forum software by © MyBB