Use routes for mulitlevel folders in CodeIgniter - php

Is it possible to use CodeIgniter to route to sub folders. My current folder setup is like this with my routes line to access my owners controller. The url i am access is www.site.com/owners/home
controllers /
login.php
owners /
home.php
profile.php
students /
home.php
profile.php
$route['owners'] = "owners/home";
I would like to put the owners and students into a seperate folder, to group them, but access them through the same url and not show the users folder. I was hoping the route below would get me where I wanted.
controllers /
login.php
users /
owners /
home.php
profile.php
students /
home.php
profile.php
$route['users/owners'] = "owners/home";

$route['owners'] = "users/owners";
$route['owners/(:any)'] = "users/owners/$1";
You got it back to front the part to the left is the bit in the URL -> the part to the right is the part that defines where the path will go in the controllers.
The second statement might be necessary to ensure controllers methods still work - depends on your routes.php file as my customised install is now heavily modified!

Related

get subdomain in htaccess and route to a new folder

Is it possible to extract the subdomain part of a domain and use it like a variable in an .htaccess file?
Example
mysub.domain.com
What I'm looking for
(*).domain.com /$
after that the "mysub" is inside the "$-Sign"
Completely I want to route to another subfolder
normally mysub.domain.com routes to the "maindist" folder
now I want to do it more dynamically, so I want to route inside the folder which has the name of the subdomain
mysub.domain.com
normal route->maindist
after htaccess route->mysub
in best case I can do that without notice of the user, so without changing the addressbar from the browser.
Hope somebody can help and explain.

redirect on home page when user write index file name in url

Bellow is my site directory structure:
project-folder/
index.php
Now I have access project using folder name like www.project-folder.com and also www.folder-name.com/index.php
Problem
If some one user enter www.folder-name.com/index.php in URL then redirect on www.project-folder.com using htaccess.
How can I do this ?
Below line is I tried in htaccess
Directory Index public/index.php
Use this on the htaccess file of www.folder-name.com
Redirect index.php http://www.project-folder.com/index.php
To not make confusion, use this kind of redirect:
Redirect /path/to/old/file/old.php http://www.redirectedwebsite.com/path/to/new/file/new.php

Routes with Codeigniter

I know how to set up routes for the most part but instead of listing out 50 or so different routes for only one thing I want to change is redundant.
I havemy file system setup like this for my codeigniter application.
-application
-controllers
-admin
login.php
register.php
dashboard.php
As of right now when I go to the following site it shows up the way it should.
http://www.mysite.com/admin/(login,register, dashboard)
However I'd like to manipulate the route to appear as the following without having to change the name of the folder in the file structure.
http://www.mysite.com/my-project/(login,register, dashboard)
Is "my-project" a permanent part of your siteurl, for every single page? Then change your base_url() in the config.
Otherwise:
if your base_url is http://www.mysite.com
and you want to go to http://www.mysite.com/my-project/login
then you set the route to be:
$routes['my-project/(:any)'] = 'admin/$1';
or if only for those three:
$routes['my-project/('login'|'register'|'dashboard')] = 'admin/$1';
SHOULD work, although I don't have a handy way to test at the moment

CodeIgniter Ion Auth pages outside auth folder

I am very new to CodeIgniter Ion Auth, I was wondering if it is possible to add pages, outside the auth folder? the pages can be accessed only if the user is logged in. The main reason why I wanted the pages outside the auth folder is that I want to get rid of the "auth" in the url. Any help would be greatly appreciated. By the way, I only tried adding pages inside the auth folder. Thanks!
The short answer is Yes, you can. But if the reason is simply to remove the "auth" in the URL, the easiest way to achieve this is by using routes settings (located in the \application\config\routes.php file)
For example, if you set;
$route['login'] = 'auth/login';
The URL to the log in page will be;
http://yourdomain.com/index.php/login
You can go a step further by adding URL suffix in codeigniter config settings ( \application\config\config.php)
$config['url_suffix'] = '.html';
so that the URL to the log in page will be;
http://yourdomain.com/index.php/login.html
You can even remove the /index.php part of the URL. See this forum here

codeigniter only pages to an existing website

My website structure somewhat looks like below
css/
lib/
js/
index.php
profile.php
products.php
checkout.php
orders.php
invoice.php
I have added a codeigniter folder in there ...
codeigniter/application/
codeigniter/application/controllers/
codeigniter/application/controllers/mycontroller.php
and other files
I can access CodeIgniter stuff by going to mywebsite.com/codeigniter/mycontroller etc fine.
However, I want to get rid of /codeigniter/ part from the URL. So I was wondering if it is possible to create a whitelist of the files which are CodeIgniter specific? For example, if the URL is mywebsite.com/mycontroller then it does CI stuff otherwise it looks for the plain PHP code file. I have only a couple of CI controllers and loads other non-CI files.
Any ideas?
I think you could use .htaccess to rewrite URL's that don't contain .php, css, lib and js. Something like:
RewriteCond %{REQUEST_URI} !^(\.php|css|js|lib)$
RewriteRule (.*) codeigniter/index.php/$1
So:
http://example.com/css/test.css
stays
http://example.com/css/test.css
(as will all requests to css|lib|js. You can append more things here for the rewrite to ignore)
http://example.com/controller/method
becomes
http://example.com/codeigniter/index.php/controller/method
You can test it out here: http://htaccess.madewithlove.be/
More on rewriting: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Short-term Solution
You can start by simply converting the index.php file into a controller and name it whatever you wish:
<?php
class New_default_controller extends CI_Controller {
public function index()
{
// home page stuff here
}
}
Alter the route.php file and set your default controller so that simply visiting your site will trigger the proper controller:
$route['default_controller'] = 'new_default_controller';
Apply the instructions for Removing the index.php file
Now calls to www.mysite.com/profile.php will access the profile.php at your root and calls to www.mysite.com/new_future_page will call your new_future_page controller.
Please let me know if any of this is confusing or you get stuck.
Optimal Solution
I wanted to leave a comment above but this would have been impossible to show as a comment.
You will have to take your PHP files and put them in the controllers folder like this:
codeigniter/application/controllers/profile.php
codeigniter/application/controllers/products.php
codeigniter/application/controllers/checkout.php
codeigniter/application/controllers/orders.php
codeigniter/application/controllers/invoice.php
Please go through and do the Tutorial before continuing any further. Specifically the Static Pages section will help you in achieving your goal.
You will have to convert your current PHP files to follow the flow of CodeIgniter

Categories