CodeIgniter Forums
CMS best practice on CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: CMS best practice on CI (/thread-55341.html)

Pages: 1 2


CMS best practice on CI - El Forum - 10-23-2012

[eluser]Mr. Pickle[/eluser]
1. REROUTING
You need to think the other way. What the user accesses IS the url.
Start with the url that the user visits (requests, or access like you said) and from there route your way to the controller you want, not the other way around around Smile

So, the user accesses/requests admin/news/add (because this is the url you want)
Codeigniter thinks it should be interpreted like this:
controller: admin
method: news
param1: add

What you want is:
controller: news
method: add

So reroute the url like this
Code:
$routes['admin/news/add'] = 'news/add';

Mind that rerouting is NOT redirecting. The user will still see /admin/news/add.
Of course the example above is pretty static, you can make it more dynamic (see earlier posts)

2. ADMIN CONTROLLER
Yes there is a pretty workable way of doing this.
I would suggest this:
1. rename the file admin.php in controllers to Admin_controller.php
2. move the Admin_controller.php file from /application/controllers to /application/core/
3. change this file so that the class is called Admin_controller and put all admin functions in this file:
Code:
<?php
class Admin_Controller extends CI_Controller
{
    public function __construct()
    {
       parent::__construct();
    }

    public function is_logged()
    {
        // ....
    }
    
    // all the other shared admin functions go here ....
}
?>

4. Make all the ADMIN controllers extend the Admin_controller. So for example the news controller (/application/controllers/news.php:
Code:
<?php
class News extends Admin_Controller()
{
    public function __construct()
    {
       parent::__construct();
       // you can for example load $this->is_logged()
       // here if you want to have this function run automatically.
    }

    public function add()
    {
         // ....
    }    
}
?>

Note that now the Admin_controller is extending CI_Controller and the normal controllers extending the Admin_Controller.
If you also have controllers outside the admin area make sure they extend CI_Controller.




CMS best practice on CI - El Forum - 10-23-2012

[eluser]Felipe Deitos[/eluser]
Man that was a really really nice post!
Appreciate that!

The 1st one still a little confuse for me but i will do some tests and try to understand better the routing system. It will be easy if i do this instead of trying understand heheheh.

The 2nd was great, just one question, i still need to have a controller/admin.php?
Then the admin.php will have like:

Code:
class Admin extends Admin_Controller()

And put all the Admin_Controller functions ?

Because i will need to call in url
/admin/method
not /admin_controller/method

Thanks again for your replys
Cheers!


CMS best practice on CI - El Forum - 10-23-2012

[eluser]Mr. Pickle[/eluser]
The Admin_controller like I set it up in my previous post is not a controller as in that it is being called via an url.

It is just a class with overall functions that the 'normal' controllers (the ones that are accessed via an url) use.
You will store all your overall admin functions (like checking of someone is logged in, do the logout, etc.) in this Admin_Controller. You can still keep your controllers/admin.php controller, for example to view the login page at /admin/login/ (where admin is a controller and login the method)
Rerouting always take place before the rest, so you can have admin paths (like /admin/login) that actually load admin as a controller and login as a method, as also have admin paths that are rerouted, like /admin/news/add ==> /news/add.

To explain a little the advantages the Admin_controller can give you:
If you for check on construct of the Admin_controller if a user is logged in, you just extend the Admin_controller at every controller that is part of your admin-system and the check if someone is logged in will automatically take place.

Code:
<?php
class Admin_Controller extends CI_Controller
{
    public function __construct()
    {
       parent::__construct();
       if( !$this->is_logged_in() )
       {
           redirect('path-to-login-page'); // where you replace this with the path to your login page
       }
    }

    public function is_logged_in()
    {
        // here the code to dermine if the user is logged in
    }
}
?>



CMS best practice on CI - El Forum - 10-23-2012

[eluser]Felipe Deitos[/eluser]
You really solved all my problems.
Now i will probably study a little bit more about reroute!
Sorry about all those questions, and thanks for all the answers!

Maybe one day i help someone like u helped me bro!

Cheers!


CMS best practice on CI - El Forum - 10-23-2012

[eluser]Mr. Pickle[/eluser]
No problem, that what's the forum is there for Smile

Reroute can be helpful to achieve certain things. If you study it more you will find out you can make it really flexible/dynamic by using (:any) and (:num), but also by using your own regular expressions.
Most of the background info to be found here:
http://ellislab.com/codeigniter/user-guide/general/routing.html

I also looked you up some more info about the Admin_Controller, because it's pretty much a common way to set up CodeIgniter (both the structure and the naming of the classes)
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
(mind that maybe certain things changed a little as the post is more than two years old, but it seems still reliable)