Welcome Guest, Not a member yet? Register   Sign In
Admin under controllers and then Admin_user under controllers/admin?
#1

[eluser]awpoon[/eluser]
I'm at a bit of a crossroads.

So I'm currently building the Admin section of my application, and I'm stuck. I currently have the following hirearchy:

- Admin (Controller)
- User (Controller)
- Create
- Change Password

and so on and so forth. My design goal is to have all the user-related functions in its own controller, and then the admin functions for another component in his own controller as well.

I come from Kohana, and over there, we'd basically have a Controller_Admin function (in Kohana, controller functions have Controller_ as a prefix for controllers and Model_ as a prefix for models) and then a Controller_Admin_User, and maybe a Controller_Admin_News, etc.

I've attempted to play around with the routes, so I was hoping routing admin/user to 'admin/admin_user' would cut it, but it seems to not. I currently have admin.php under controllers and an admin directory which has admin_user.php. Is there anything that I am missing, or can I have direction to the best way to implement this?
#2

[eluser]TheFuzzy0ne[/eluser]
If I'm understanding what your saying, I'd go for the following structure:

./admin (directory)
./admin/home.php (or whatever the name of your default controller is)
./admin/user.php

If you want to call any method in the home controller, you'd need to call it like this: www.mysite.com/admin/home/<some_method>.

You can add some routes if you want, but you'd probably need to add a route for each method in your default controller.

Hope this helps.
#3

[eluser]awpoon[/eluser]
[quote author="TheFuzzy0ne" date="1366029237"]If I'm understanding what your saying, I'd go for the following structure:

./admin (directory)
./admin/home.php (or whatever the name of your default controller is)
./admin/user.php

If you want to call any method in the home controller, you'd need to call it like this: www.mysite.com/admin/home/<some_method>.

You can add some routes if you want, but you'd probably need to add a route for each method in your default controller.

Hope this helps.[/quote]

I suppose I can have a home.php, I didn't think of that. The hirearchy was supposed to be:

./admin.php
./admin/user.php

Which seems to have some conflicts.
#4

[eluser]TheFuzzy0ne[/eluser]
Indeed. You should see the Loader class. Basically, it will search to see if 'admin' is a directory, before searching for a controller named 'admin'. If it finds the directory, it looks in there for a controller matching the next segment of the URI. admin.php will never actually be loaded, unless you place it within the admin directory, and call it using the URL www.mysite.com/admin/admin.

It's possible to do it the way you want. You'll just need to extend the Load class to have it work the way you want, but it shouldn't really be necessary.

My admin sections follow the same pattern. The Home controller only has one method in it - index().

So www.mysite.com/admin will load the index() method of the home controller, and www.mysite.com/admin/controller/method will load other controllers.
#5

[eluser]awpoon[/eluser]
[quote author="TheFuzzy0ne" date="1366042925"]Indeed. You should see the Loader class. Basically, it will search to see if 'admin' is a directory, before searching for a controller named 'admin'. If it finds the directory, it looks in there for a controller matching the next segment of the URI. admin.php will never actually be loaded, unless you place it within the admin directory, and call it using the URL www.mysite.com/admin/admin.

It's possible to do it the way you want. You'll just need to extend the Load class to have it work the way you want, but it shouldn't really be necessary.

My admin sections follow the same pattern. The Home controller only has one method in it - index().

So www.mysite.com/admin will load the index() method of the home controller, and www.mysite.com/admin/controller/method will load other controllers.[/quote]

Yeah, fair enough. I guess I'll settle for admin/home like you suggested, I don't mind that.
#6

[eluser]awpoon[/eluser]
Another question (maybe you'll know this one).

Is there a way to prefix only the Model classnames? I've noticed I'd have a User class in Models and User in controllers/admin.
#7

[eluser]TheFuzzy0ne[/eluser]
No, there's not. This is a restriction imposed by PHP which prevents you from defining the same class more than once. To get around this restriction, I suffix all of my models with '_m':

Code:
class User_m extends CI_Model {

When you load it, you can specify the name of the property you want it to be assigned to, so:
Code:
$this->load->model('user_m', 'user');

$user = $this->user->get($user_id);

Hope this helps.
#8

[eluser]awpoon[/eluser]
So then if I have the following in my controllers directory:

./c_admin.php
./admin/c_user.php
./admin/c_home.php

Where user and home extend C_Admin, which has a remap function (that enforces login) and a constructor that loads a user model.

How would I reroute the entire URL/admin/user to the admin/c_user function? Routing admin/user to the c_user class does not seem to work.

$route['admin/user'] = 'c_user';

Nor does this.

$route['admin/user'] = 'admin/c_user';

IIRC, does the latter route the

I've also noticed that going to admin/c_user in its current state will give me a 500 error, with the logs saying the following:

2013/04/19 07:34:33 [error] 1176#0: *13 FastCGI sent in stderr: "PHP message: PHP Fatal error: Class 'C_Admin' not found in /var/www/application/controllers/admin/c_user.php on line 3" while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /admin/c_user/create HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "localhost"

Do I have to do a require to load C_Admin? That is, does CodeIgniter not load it for me automatically?
#9

[eluser]TheFuzzy0ne[/eluser]
If you want to extend anything that is not already loaded, is not part of the core, or is not loaded by convention (i.e MY_Model, MY_Controller, MY_Email etc...), you will need to include() it yourself (at the top of your controller file). You could overload the __autoload() magic function, but with CodeIgniter's current file/directory structure, you'd potentially end up having to iterate through a lot of directories.

However, by prefixing all of your controllers with 'c_', I suspect you're going to run into problems with routing, and you'll end up having to add a lot of routes. Extending the Router class might be an option to overcome this, but I don't see why you should need to modify the framework when suffixing your models names will achieve the same goal, without having to modify the framework.

By simply suffixing your model, it make things a lot easier. No routes are necessary, and you have the ability to give your models custom alias' on-the-fly.
#10

[eluser]awpoon[/eluser]
[quote author="TheFuzzy0ne" date="1366383790"]If you want to extend anything that is not already loaded, is not part of the core, or is not loaded by convention (i.e MY_Model, MY_Controller, MY_Email etc...), you will need to include() it yourself (at the top of your controller file). You could overload the __autoload() magic function, but with CodeIgniter's current file/directory structure, you'd potentially end up having to iterate through a lot of directories.

However, by prefixing all of your controllers with 'c_', I suspect you're going to run into problems with routing, and you'll end up having to add a lot of routes. Extending the Router class might be an option to overcome this, but I don't see why you should need to modify the framework when suffixing your models names will achieve the same goal, without having to modify the framework.

By simply suffixing your model, it make things a lot easier. No routes are necessary, and you have the ability to give your models custom alias' on-the-fly.[/quote]

Oh, never mind. I misread why you suffix it. Makes sense (my apologies).




Theme © iAndrew 2016 - Forum software by © MyBB