PHP Separate admin controllers CODEIGNITER - php

I have some basic controllers and admin controllers, I'm trying to separate them in folder to avoid things like this one:
controllers/user.php -> general users
controllers/a_user.php -> for admin users
I've read some things about route but couldn't find a way to do that.
Thanks in advance for any help.

Create a subfolder inside controllers folder and place your admin controllers in there.

Related

Codeigniter HMVC and standart Codeigniter controllers

I have admin module (modules/admin) with controller admin (modules/admin/admin).
I need put new controllers of admin to Codeigniter controllers folder (application/controllers/admin/new_controller), but when i go to site.com/admin/new_controller – 404. Why I need it? I want to admin module was every customer the same, but the other controllers must be unique for each project, thats why I need to put new controllers to application/controllers/admin/ folder.
Is it possible to do this?
Codeigniter 3 and Wiredezignz HMVC (https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc)
Ok, I don't know if it's the right decision, but I change name of folder: application/controllers/admin to admin_unique
and add to application/configs/routes.php two lines, example:
$route['admin/(login|logout|index|settings|users)'] = 'admin/admin/$1';
$route['admin/(.*)'] = 'admin_unique/$1';

Codeigniter - Forgot to make an admin folder in controller and just created an admin.php controller

I created a controller called admin.php and placed it in the controllers folder. I was working on this "users" system where I was coding the things to edit, create, manage users. I made functions like users, edit_user, etc so when visited, it looked like http://localhost/admin/users, http://localhost/admin/edit_user
I am finally done that part.
Now, I am working on the site section of my panel where I will be able to edit the site. I just realized that I needed to create an 'admin' folder in the controllers folder so I could make a brand new users.php controller and place all my functions for managing users there.
I have no idea what to do. I don't want to put everything from the admin panel in a single admin.php file. I tried creating a new admin folder but I realized I have to change the url's to everything.

structure for Admin and user panel in CodeIgniter?

Is this the structure to follow: for creating a structure for user and admin
is anything like this???
application ->
controllers
->manage folder
->admin controller files
->user controller files
models->
all model files are created her e in this single folder
views->
>manage folder
->admin views files
->user views files
Then what about the default controller we set in the config.php? And again in the views folder we used to create subfolders as pages and templates, right?
So please help me to form a structure by clubbing all these points.
Basically there is no standard structure. If You wish to separate admin panel from user interface - You can do this in Your own way.
HMVC will help you to achieve this. For my solution, I use below format:-
application ->
modules ->
module_name
under module_name folder I use below:-
controllers ->
manage ->
admin controller file
controllers ->
user controller file
models -> model file
views ->
manage -> admin views file
views ->
user views file
For my projects I create folders named "admin" in controller , model and view folders and put administration code there.
but this practice can leads to application vulnerabilities. "www.exsample.com/admin" can be a very easy target for automated hacking tools. so pickup random hard to guess string and make a redirect using .htaccess or routes.
eg:
"www.exsample.com/Gh567R"

Location of admin controller methods

I am building a Laravel 4 application and I am trying to sort out where my admin controller functions should go.
eg my admin user view, edit and update functions.
Previously in my User controller, I would have
function getIndex()
{
// Get method for normal users
}
function getAdminIndex()
{
// Get method for admin users
}
I would then have in the routes /users -> getIndex() and /admin/users -> getAdminIndex()
however, is this ideal?
The reason is it makes my routes file quite large as I have to specify every route.
With things like Blog posts, and Products, should I have one controller for logged out / user access, and then a separate controller, in an admin folder for just the admin functions?
Is there some open source projects I can look at?
You should separate your regular controllers from your admin controllers. It's a matter of personal preference how you do it: you can create folders inside your controllers folder, or maybe you could create –almost– independent modules with controllers on their own.
For the first option, take a look at this project:
https://github.com/andrew13/Laravel-4-Bootstrap-Starter-Site
For the second one, which is a bit more complex, you can get inspired from this post by Ryan Tablada:
http://ryantablada.com/post/juggling-larger-laravel-applications
I personally prefer this last one if the project is medium/large size, and the other one if it's a small project.
You can also try Laravella, a CMS, CRUD, Bootstrap, Uploader etc. framework for Laravel.
https://github.com/laravella/laravella/releases
If your project is small, then you can use Resource Controller.

Best way to make Admin pages in CodeIgniter?

I'm working on an app in CodeIgniter, and I want to have admin pages for several of the objects in the application, and I'm wondering what would be the better way to put these into an MVC structure.
Idea 1:
In each controller, have an admin function, and add all of the admin pages I would like into that function.
example URL: domain.com/articles/admin
Idea 2
Make a new admin controller, which would have to reference many different models, and put all of the admin pages in there.
example URL: domain.com/admin/articles
Which way would be better?
Edit for clarification: By admin functionality, I mean being able to do the basic CRUD actions on any object, and be able to display a list of all of said object.
Definitely a different controller at least!
I used to think that I could keep all my admin functions in a single controller, but as my programs grew, I realized that I needed multiple controllers in my administration section.
So, I created a folder inside my controllers folder with the name "admin" and put all my administrative controllers in there. So my folders would look something like:
application
controllers
front.php
welcome.php
admin
dashboard.php
useradmin.php
etc...
One problem this creates, however, is when you type http://mysite.com/admin in your browser, it returns a 404 page. So, go to your "application/config/routes.php" file and add a custom route:
$routes['admin'] = 'admin/dashboard/index';
I'll echo Justin in keeping it part of the individual controllers.
You should setup some kind of authorization system that the individual controllers can use to so who is logged in (username) and what access they have (admin/member/etc). Here's a SO thread on CodeIgniter Auth Classes.
The view would then conditionally show the appropriate links, and the controller would enforce the policy by checking the auth before passing any data to the model or rendering an edit view. On unauthorized access an error could be rendered, or simply render with the non-editing view.
This approach seems to make the most sense (at least to me) because all the functionality is stored in the individual controller. Keeping admin functions in a single admin controller means you'll have to manage two controllers (the admin, and the actual controller) every time you add somethign new (or remove something).
If you're concerned about putting auth checking in every controller, you could create a generic controller class with all the auth setup, then have your controllers extend it. In the end the individual controller auth check could be as simple as:
function edit()
{
if(!$this->auth()){
//display auth error, or forward to view page
}
}
Of course some kind of ACL implementation would make this better, but I don't believe CodeIgniter has an 'official' ACL.
It's a good idea to have an admin folder in the controllers folder wherein you can access your administration e.g. yoursite.com/admin/users.
All your administrative needs will be there and all methods will be protected by checking user privileges like so:
if ( ! $this->auth->logged_in(array('login', 'admin')))
{
$this->session->set_flashdata('message', 'You do not have access to view this page');
redirect('admin/users/login');
}
Then all controllers outside the 'admin' folder will - depending on your type of site - will only be for viewing, etc.. no administrative portions.
Idea 2 is better.
system/application/controllers/admin
You keep all your admin controllers here.
Here is an extensive guide to the pro's and con's of each method:
http://philsturgeon.co.uk/news/2009/07/Create-an-Admin-panel-with-CodeIgniter
Depending on what you mean by 'Admin' functionality...typically, this is thought of as an 'Edit' view.
And typically, you use the existing controller to serve the 'Edit' view allowing the authorized users to make the edits (in your case, Admin users only).
Looks like a personal choice, i love having everything centralized so the admin controller would be my bet.
That way i wouldn't have to open up 5 different controllers while modifying admin tasks.

Categories