Codeigniter controllers for subpages? (New to Codeigniter please help) - php

I am new to codeigniter so please excuse my stupidity.
I am building a client portal and I have my admin / client login setup I am currently working on the admin area first so I have my controller for admin setup and it loads a view stored;
views (folder) -> admin (folder) -> admin-view.php
I wanted to keep all my admin panel views in the folder admin within the views folder.
Anyway so I have setup my links to pages in the admin panel like;
Client Companies<br/>
Client List<br/>
Rigs / Platforms<br/>
Now I understand that when you call a url like I have /admin/ is the controller and /company/ is the function which is called.
Now I would like to call a controller for company for this page because I have a lot of code in the company controller which defines what views and what data to pull for the company page of the admin panel.
Right now my function in admin for company looks like;
function company(){
log_message('debug', 'company_view Function Ran');
$data['page_title'] = 'Bomar Client Portal - Admin Area';
$this->load->view('admin/header', $data);
$this->load->view('admin/admin_navigation');
$this->load->view('admin/company_view', $data);
$this->load->view('admin/footer');
}
which I would like to replace just to call the company controller, I have looked into similar questions for calling controllers from controllers and everyone recommends against it.
So if I shouldn't call the controller for company from the admin controller how would I keep the url to point to /admin/company/
I know I could easily point to /company/ and it would render the page using the controller for company but I would like to keep the admin bit in the url if you understand where im coming from? is this a case of "you can't do it stop being so picky!"?

read up on codeigniter URI routing here:
http://ellislab.com/codeigniter/user-guide/general/routing.html
allows you to set up redirects to other controllers

Related

codeigniter inherints controller from controller

I have Admin.php as contrller for dashboard,signin and logout
but in my application
the admin user can edit news or add news I wanna separate news & admin controller and news controller work under admin
to understand my I wanna URL like http://localhost/nkmf/admin/
after admin if I write news get my news dashboard
and the URL be Like: http://localhost/nkmf/admin/news/
and after news if I enter
editeNews open editing form for selected news
and URL be like http://localhost/nkmf/admin/news/editNews?id=
create sub folders named admin,news in controller folder
move your admin.php controller to the new subfolder named admin.
so you can access it by
http://localhost/appname/admin/controller_name/function_name
As far as I have understood from your question the way you can achieve this is by either creating or overriding the .htaccess file or routes method. In which define your required url which leads to the the particular filename.php file. But for this you must know how to access and define the .htacess file or the routes class. If you are capable of handling htaccess file with regex, then you got it
Remember the codeIgniter url structure is http(s)://domain_name/sub_division/controller_name/function_name/parameter

multi user website in codeigniter

I want to build a multi user website where my users register and their account is created.
After sign up the user gets a specific url to build his website. Like. Mysite.com/user1
Now the user can login to the control panel and publish his data. Eg he has created a faq page.
His faq page should be available like
Mysite.com/user1/faq
I've done everything but this faq page building does not work at all.
I check the user for existence in the DB and route to his page in the routes.php file
:any = home/loader/$1
And in loader.php I fetch the details and display his welcome page, for the faq page, I have created a function in the loader.php controller as
Faq()
And built a custom view for it but now when I try to access the page it only loads the welcome page.
I tried all methods but it does not work.
I suggest you have a look at the Tutplus
http://code.tutsplus.com/tutorials/basecamp-style-subdomains-with-codeigniter
It explains how to work with wildcard routing in codeigniter

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.

codeigniter passing the same information (like login status) to all the views

I'm using DX_AUTH to handle autentication in my codeigniter app.
I want to display in each page the login status, I'm used to develop my view throught template inheritance.
I'm looking for a way to access login information from the views without passing it each time.
Views shouldn't engage themselves in "lookups". Why not make a template view that can be passed your authentification information. Then, build a template controller, from which all other controllers inherit, that passes the authentification info to the template view. That way, you only write the code once for the template view and the template 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