Welcome Guest, Not a member yet? Register   Sign In
Structure Dilema
#11

[eluser]TheFuzzy0ne[/eluser]
But you only have two controllers, right? Post and View?
#12

[eluser]drewbee[/eluser]
Yes in that particular setup I only have two controllers. Sorry it kind of contradicted what I originally was saying I was doing. lol...

HOWEVER, at my root I have a few forms like contact.html, report.html, tos.html, privacypolicy.html etc all of these are their own controller.

I really need to get my butt in gear and create a mini-cms so I can just do something along the lines of viewpage/contact.html etc. I have to many controllers for static content.
#13

[eluser]TheFuzzy0ne[/eluser]
Here's my extended Router library. It's a very simple change that only adds one extra step onto what CodeIgniter already does. That is, all of the checks fail, one last attempt it made to load the default controller from within the subdirectory, and the specified method is executed if it exists. I've only added a single block of code. Personally, I'd like to have seen CodeIgniter do this from the start. I can now place all of my methods into the forums controller. Sweet!

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

class MY_Router extends CI_Router {
    
    function _validate_request($segments)
    {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {        
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);
            
            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    # START EDIT #
                    if (file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                    {
                        $this->set_class($this->default_controller);
                        $this->set_method($segments[0]);
                        $segments = array_slice($segments, 1);
                        return $segments;
                    }
                    # END EDIT #
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');
            
                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            
            }

            return $segments;
        }

        // Can't find the requested controller...
        show_404($segments[0]);
    }

}
#14

[eluser]slowgary[/eluser]
But I thought you didn't want all of your methods in one controller? If that was the case you could just use _remap();
#15

[eluser]TheFuzzy0ne[/eluser]
No, I wanted to group them together in files, but I wanted to avoid _remap, and routes. It just makes sense to me that CodeIgniter should check the default controller for the method as a last resort. Now my structure works as I'd expect it to:

Code:
controllers
+-forums
  +-homepage.php (default controller)
  +-member.php

Any requests that aren't for the "member" class, will be redirected to the default controller.
#16

[eluser]slowgary[/eluser]
So if you have:
Code:
controllers
+-forums
  +-homepage.php (default controller)
    view_thread();
    view_latest_threads();
    search();
  +-member.php
    login();
    view_profile();

And someone visits:
Code:
http://www.domain.com/forums/member.php/view_thread/1
Will that work now? Sorry for being dumb but I don't really understand the router or your extension of it.
#17

[eluser]Colin Williams[/eluser]
You should have one controller per resource, and you should not let grouping change that. It seems clear to me that you have a class of resources that make up the forums. What are your resources? Members and Threads. Yet you've yet to create a Thread controller. With that, you would have

Code:
controllers
+-forum
  +-thread.php (default controller)
  +-member.php

So, now you have URIs like /forum/thread/view/241, /forum/thread/create, etc. (I don't see how Homepage is a resource in your application, yet /forum/thread/index makes sense as the default for a forum.)

One last point. You might also consider Forums a resource in your application (manage multiple forums) but you get a bit stuck by having the 'forum' folder, which gets you back to the dilemma of the Router class looking for controllers when it should be looking for functions. What I like to do in this case is have a Core controller (which basically serves as a Forum controller, just by a different name). And if you just don't want to have URIs like /forum/core/add, /forum/core/edit, etc, then there aren't going to be so many functions in Core.php that you can't just set up a simple route for each ($route['forum/add'] = 'forum/core/add'; etc). Overloading the Router class for this just doesn't seem like the most simple way to solve the problem.
#18

[eluser]TheFuzzy0ne[/eluser]
Well, the URL would be http://www.domain.com/forums/member/view_thread/1.

Everything works just like before, the only addition is that if the page is not found, one last attempt is made to open up the default controller in the subdirectory (if it exists, and a method has been specified), and then the right method is called if it exists in the default controller.

One thing I think I need to do, is have CodeIgniter do the same when the default controller is not in a subdirectory.

At the current time, it only really makes sense to have an index method in your default controller, unless of course you plan on calling your default controller by name in the URL. Assuming my default controller is "Homepage", http://www.mysite.tld/subdir/homepage/some_method becomes http://www.mysite.tld/subdir/some_method and when I've made the next modification http://www.mysite.tld/homepage/some_method will become http://www.mysite.tld/some_method.

If you tell me the name of your default controller, I can throw a demo together for you so you'll see what I mean.
#19

[eluser]TheFuzzy0ne[/eluser]
[quote author="Colin Williams" date="1238574214"]You should have one controller per resource, and you should not let grouping change that. It seems clear to me that you have a class of resources that make up the forums. What are your resources? Members and Threads. Yet you've yet to create a Thread controller. With that, you would have[/quote]

I was planning on having things like view (view forum), view_thread, reply, new, etc all in my homepage.php.

[quote author="Colin Williams" date="1238574214"]So, now you have URIs like /forum/thread/view/241, /forum/thread/create, etc. (I don't see how Homepage is a resource in your application, yet /forum/thread/index makes sense as the default for a forum.)[/quote]

Homepage is the name of my default controller for my Web site. This is pretty much my dilemma. I'm trying to keep homepage (or index, or any other default controller name), out of the URL. IMHO, the default controller should be called anyway, if the page cannot be found, after all, it is the default.

This app is not just a standalone forum. It's an entire Web site. Perhaps I should be building the Web site and forums as two separate apps?

[quote author="Colin Williams" date="1238574214"]
One last point. You might also consider Forums a resource in your application (manage multiple forums) but you get a bit stuck by having the 'forum' folder, which gets you back to the dilemma of the Router class looking for controllers when it should be looking for functions. What I like to do in this case is have a Core controller (which basically serves as a Forum controller, just by a different name). And if you just don't want to have URIs like /forum/core/add, /forum/core/edit, etc, then there aren't going to be so many functions in Core.php that you can't just set up a simple route for each ($route['forum/add'] = 'forum/core/add'; etc). Overloading the Router class for this just doesn't seem like the most simple way to solve the problem.[/quote]

Thanks for that, but I was looking to avoid routes if possible as I don't feel they should be needed. I'm not entirely sure what you mean by "You might also consider Forums a resource in your application (manage multiple forums) but you get a bit stuck by having the 'forum' folder, which gets you back to the dilemma of the Router class looking for controllers when it should be looking for functions". If you could please give me a clear example, I should be able to test it. I don't see the problem however, as I've not changed how the router class loads controllers, I've just added an extra step to the end which is only executed when the router class would normally fail and display a 404 error.




Theme © iAndrew 2016 - Forum software by © MyBB