Hide Codeigniter controller name from URL with same routes - php

i'm just getting started at codeigniter, i want to hide controller name from URL with same routes setup.
i have 3 controllers which are students, staff, teachers having same function called home, this won't work obviously
$route['home'] = 'students/home';
$route['home'] = 'staff/home';
is there any way to accomplish this? i have session data using codeigniter session class containing user type so i tried something like this
session_start()
$route['home'] = $_SESSION['user_type'].'/home';
but i cant get the session data, maybe its using codeigniter session class?? so, how can i get the data? or is there other solution?

Perhaps you should write a common controller and disperse by your second URI parameter:
home/students or home/staff
$route['home/:any'] = "home";
and home controller's index method:
public function index()
{
$type = $this->uri->segment(2);
switch($type){
case "student":
$this->student();
break;
case "staff":
$this->staff();
break;
default:
$this->some_other_method();
break;
}
}
Obviously you would create a student and staff method and handle things differently if need be.
A side note - why do you want to conceal the controller's name? It's not like that's a security hole or anything.

Related

Codeigniter 2 Controller Fall Through to a different controller

I have a site I am working on that has a URL structure that looks like:
example.com/category-name
This currently goes to a 1000 line long controller called pages that was built years ago and has never been changed. I am inserting a new controller above this in the route that looks like:
$route['(:any)'] = 'new_controller/load/$1
$route['default_controller'] = "pages";
The idea is for new_controller to work like this:
example.com/page-name
This looks up page-name in a database and displays some text made by business people in the admin of the site.
The problem comes from that if page-name is not found in this table, for legacy purposes I must fall through new_controller/load and then use the pages controller normally.
For SEO reasons I can't change the URL structure, it has to remain example.com/category-name and they want it to share the same URL structure example.com/page-name because it's shorter(I tried to convince them to do something like example.com/page/page-name)
From new_controller/load/$1 how would I then call say pages/view/$1 ?
Thanks in advance it is going to really save my butt at work haha
In your route config you can use the config below. new_controller controller handles all page requests so long as their uris don't contain pages/view/.* which pages controller handles.
$route['pages\/view\/(.*)'] = 'pages/view/$1';
$route['(?!pages\/view)(.*)'] = 'new_controller/load/$1';
In new_controller controller - you can do something like the code below so if the slug doesn't exist in database it will be handled by pages controller instead.
public function load($slug)
{
if(/*slug in database*/){
echo "Slug content from database";
}
else { // slug not in database
redirect(site_url('pages/view/'.$slug)); // You can use this
echo file_get_contents(site_url('pages/view/'.$slug)); // or this
$this->load->view('pages_template'); // or this
// or semething else.
}
}

How to use multiple controllers and model in codeigniter 3.1.4?

I am very new to codeigniter and i am using the version 3.1.4.
I have 2 users in my system and i wanted to use 2 different controllers in the same system, since i felt that using one single controller will have too many functions in it.
what i found as solution was similar to these: How do you use multiple controllers in CodeIgniter?
i dont find any $route['(:any)'] in my routes file.
Please help me solve this with every step since i am very new.
I want to know how to route it and how to call the function (in both the controllers) in a view page as well as model.
Also i need help in using 2 models.(for that i think i just need to mention the model name while calling the fucntion in model)need advice.
You can use routing for all user to redirect to same controller with the routing rule:
$route['users/(:any)'] = "users/index/$1";
for example-
I have two user manager and admin
http://localhost/project/users/manager/create
http://localhost/project/users/admin/create
both request redirect to users/index and now you can get value of the function using
$func = $this->uri->segment(3, 'list');
$user_type = $this->uri->segment(2, 0);
Now use switch case for call a function
switch ($func) {
case 'create':
$this->create($user_type);
return;
default:
$this->view($user_type);
return;
}

How to achieve this kind of url routing in codeigniter

i want to achieve url routing something like www.example.com/alicia
suppose alicia is not a class name or method name just something like passing data in url and with some class i want to access this and want to use it for further process.How i can use it ? Thanks in advance.
You can use Codeigniter's built-in routing, the file route.php is located in your config folder.
there you can add:
$route['alicia'] = 'welcome/index/something';
$route['alicia/:any'] = 'welcome/index/someotherthing/$1';
then in your controller, for example welcome you just create a function like:
public function index($page = null){
if($page=='something'){
// do what you need to do, for example load a view:
$this->load->view('allaboutalicia');
}
elseif ($page=='someotherthing'){
// here you can read in data from url (www.example.com/alicia/2017
$year=$this->uri->segment(2); // you need to load the helper url previously
}else{
// do some other stuff
}
}
documentation on routing and on urlhelper
edit after comment:
in case your uri segment is representing a variable, like a username, then you should use a uri scheme like www.example.com/user/alice and create your route like:
$route['user/:any'] = 'welcome/index/user';
then in your controller welcome
public function index($page=null){
if($page=='user'){
// do what you need to do with that user
$user=$this->uri->segment(2); // you need to load the helper url
}
else{
// exception
}
}
This could be tricky because you don't want to break any existing urls that already work.
If you are using Apache, you can set up a mod_rewrite rule that takes care to exclude each of your controllers that is NOT some name.
Alternatively, you could create a remap method in your base controller.
class Welcome extends CI_Controller
{
public function _remap($method)
{
echo "request for $method being handled by " . __METHOD__;
}
}
You could write logic in that method to examine the requested $method or perhaps look at $_SERVER["REQUEST_URI"] to decide what you want to do. This can be a bit tricky to sort out but is probably a good way to get started.
Another possibility, if you can think of some way to distinguish these urls from your other urls, would be to use the routing functionality of codeigniter and define a pattern matching rule in the routes.php file that points these names to some controller which handles them.
I believe that the default_controller will be a factor in this. Any controller/method situations that actually correspond to a controller::method class should be handled by that controller::method. Any that do not match will, I believe, be assigned to your default_controller:
$route['default_controller'] = 'welcome';

Codeigniter - views in subfolders routes

For organizational purposes I need to have subfolders in my views directory. For example for managing login / registration: views/login_reg/login.php and views/login_reg/register.php.
In my URI want to see: www.mysite.com/login or www.mysite.com/register instead of www.mysite.com/login_reg/login.
I have tried the following in routes:
$route['(:any)/login'] = 'login_reg/login';
Which does not work.
EDIT:
A simple example to elaborate further:
//Controller: login_reg.php
class Login_reg{
function login(){
$this->load->view('login_reg/login');
}
function register(){
$this->load->view('login_reg/register');
}
}
//Routes:
$route['login_reg/(:any)'] = 'login_reg/$1';
So my URI looks like this: www.mysite.com/login_reg/login, or www.mysite.com/login_reg/register
So I want one controller to manage all login / registration related views. But I do not want to see the controller name in the URI. Is this possible? Or is the best approach to have a controller for login, a controller for registration, a controller for password changing etc? This seems a bit excessive. I want to have my files well organised into related directories as this is part of a very large site.
Here you don't need routing at all.
Simply add your folder name while loading view.
For example your controller name for login is like
function login()
{
//code goes here
$this->load->view('login_reg/login');
}
and similar for register.
Hope this helps you.Feel free to let me know if you further queries.
--- Update ---
To hide controller name make chanes in route.php like this;
$route['login'] = 'login_reg/login';
$route['register'] = 'login_reg/register';
and your url will go like this now :
http://yourwebsite.com/login

Codeigniter multiple controllers vs many methods?

I have a site that has a lot of pages that lye at the root (ex. /contact, /about, /home, /faq, /privacy, /tos, etc.). My question is should these all be separate controllers or one controller with many methods (ex. contact, about, index within a main.php controller )?
UPDATE:
I just realized that methods that are within the default controller don't show in the url without the default controller (ie. main/contact wont automatically route to /contact if main is the default controller ). So you would need to go into routes and override each page.
If all of these are just pages, I would recommend putting them into a single controller. I usually end up putting static pages like this into a 'pages' controller and putting in routes for each static page to bypass the '/pages' in my URLs.
If they are share the same functionality, so they should be in the same controller.
for example, if all of them are using the same model to take content from, so, one controller can easily handle it.
Why in one controller? because you always want to reuse your code.
class someController{
function cotact(){
print $this->getContentFromModel(1);
}
function about(){
print $this->getContentFromModel(2);
}
function home(){
print $this->getContentFromModel(3);
}
private function getContentFromModel($id){
return $this->someContentModel->getContentById($id);
}
}
(instead of print, you should use load a view)
See in my example how all of the function are using the same getContentFromModel function to share the same functionality.
but this is one case only, there could be ther cases that my example can be bad for...
in application/config/routes.php
$route['contact'] = "mainController/contact";
$route['about'] = "mainController/about";
$route['home'] = "mainController/home";
$route['faq'] = "mainController/faq";
$route['privacy'] = "mainController/privacy";
and you should add all of these methods within the mainController.php
You can also save the content of the pages in your database, and them query it. For instance, you can send the url as the keyword to identify the page content
$route['contact'] = "mainController/getContent/contact";
$route['about'] = "mainController/getContent/about";
$route['home'] = "mainController/getContent/home";
$route['faq'] = "mainController/getContent/faq";
$route['privacy'] = "mainController/getContent/privacy";
in this case you only have to create one method named "getContent" in the controller "mainController" and this method will look something like this:
class mainController extends CI_Controller
{
public function getContent($param)
{
$query = $this->db->get_where('mytable', array('pageName' => $param));
// then get the result and print it in a view
}
}
Hope this works for you
The page names you listed should probably be different methods inside your main controller. When you have other functionality that is related to another specific entity, like user, you can create another controller for the user entity and have different methods to display the user, update the user, register the user. But its all really a tool for you to organize your application in a way that makes sense for your domain and your domain model.
I've written a blog post about organizing CodeIgniter controller methods that might be helpful to you. Check it out here: http://caseyflynn.com/2011/10/26/codeigniter-php-framework-how-to-organize-controllers-to-achieve-dry-principles/

Categories