Welcome Guest, Not a member yet? Register   Sign In
Sub category
#1

[eluser]Fr3aked0ut[/eluser]
Hi again,

Now I'm trying to make a sub-category.

What do I mean? lets say that this is my admin.php:
Code:
class Admin extends Controller {

    function Admin()
    {
        parent::Controller();
        $this->load->helper('url');
    }
    
    function index($page)
    {
$output = 'header...';
        include($page. '.php');
$dev = new $page();
$dev->admin =& $this;
$dev->auto_run();
    $output .= $dev->output;
$output .= 'footer..';
$this->output->set_output($output);
        }
Now, to include the page new_post.php the URL will be:
http://site.com/admin/index/new_post

What I want to do is to make a sub category to new_post.
I want the URL will be:
http://site.com/admin/index/new_post/sub_category

and the sub_category wont be a file, it will be a function in new_post.php

How can I do that? (with CodeIgniter of course)
#2

[eluser]darkhouse[/eluser]
There are 2 ways to do this. The way I would do it is would mean you need to change things up a bit. Instead of having a file called admin.php, you need to make it a folder called admin. Then in that you will have your default_controller.php which will handle your login stuff, etc. Now you can create your new_post.php file within the admin folder, and put the sub_category method in that file. This uses CI's natural routing the way it was intended, folder/file/method, so your url would be admin/new_post/sub_category. You don't need the index uri segment.

The other way to do it is, which is a lot easier, but I think it can lead to bad habits, is to create a custom route in the application/config/routes.php file. Just add
Code:
$route['admin/index/new_post/sub_category'] = "new_post/sub_category";

The key to CI is proper organization, or you'll be forever creating custom routes and eventually probably run into some problems. Organize things better and take advantage of CI's conventions so you don't need to create as many custom routes.
#3

[eluser]Fr3aked0ut[/eluser]
Where should I make the folder? the default_controller.php is extends to the class Controller...

EDIT:
I Tried to make the folder you was talking about(admin) in the folder 'controllers'(system/application/conterllers) and named admin.php as index.php, and get in within the URL:
http://site.com/admin/index/method

Didnt work out. (404 ERROR)
#4

[eluser]drewbee[/eluser]
Yes -- you can't call methods other then index() in a default controller. You will have to create a new controller that is not the default.
#5

[eluser]Fr3aked0ut[/eluser]
Huh? that's not what I asked.
#6

[eluser]drewbee[/eluser]
*double post, oops please delete*
#7

[eluser]drewbee[/eluser]
Back to your original post, you would have to create a index folder under admin, create a controller named New Post, and a method within that called subcategory.


I think your heading down a path that isn't in the norm way of handling things in the way CI does it.

check out the _remap function, might be of interest to you.

You can also use parameters to make a function call... but like I said your going on a kinda weird way about this:

Code:
class Admin extends Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index($param1 = '', $param2 = '')
    {
        if ($param2 == 'sub_category')
        {
            $this->sub_category();
        }
        else
        {
            $output = 'header...';
              include($page. '.php');
            $dev = new $page();
            $dev->admin =& $this;
            $dev->auto_run();
              $output .= $dev->output;
            $output .= 'footer..';
            $this->output->set_output($output);
        }
    }

    function sub_category()
    {
        // Do whatever
    }
}
#8

[eluser]Fr3aked0ut[/eluser]
First of all, thank you for helping.


I made it, tommorow I will paste up my code.


Thanks! Smile
#9

[eluser]darkhouse[/eluser]
[quote author="drewbee" date="1235690536"]Yes -- you can't call methods other then index() in a default controller. You will have to create a new controller that is not the default.[/quote]

Well, you CAN call methods in default controllers. My default controllers are always called 'home', so if I had an admin folder, I would call other methods in home file like this: admin/home/login ... or you can always setup a route, like $route['admin/login'] = 'admin/home/login';

My intention was to get the structure setup to what I think is 'proper'. I think there should be an admin folder, with a default controller, like home.php to handle the login, and then a new_post.php file that has a sub_category method. While I don't know exactly what the task at hand is, this is how I would set things up myself, based on the requirement that new_post is a controller file and sub_category is a method within.

However, thinking about it now, it looks like new_post might be part of a blog system or something similar, and in that case I would have a blog controller with a new_post method, and sub_category would either be another method in the blog controller, or a parameter of the new_post method, like function new_post($sub_category){... depends on what the purpose of sub_category really is.

Anyways, these are only my suggestions based on what I think is right and how I would do things myself. I try to keep things as clean and organized as possible so it's easier to go back to a project 6 months down the road and not have to 're-learn' everything you developed, and make it easier to change or add on new functionality.
#10

[eluser]Colin Williams[/eluser]
The whole idea of an "admin controller" just doesn't make sense to me, unless of course it is a controller that allows you to manipulate "admins" (which probably wouldn't make sense, either, because that should be handled by a "user" controller...)




Theme © iAndrew 2016 - Forum software by © MyBB