Routes with Codeigniter - php

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

Related

Use routes for mulitlevel folders in CodeIgniter

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!

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

Accessing multiple applications in Code Igniter project

I have two applications in one Code Igniter project.
application
--PW1
--PW2
This is the directory structure. I changed the application_folder in index.php
$application_folder = 'application/PW2';
This works fine. I followed this example.
What I would like to do is access the second application from the first one. How can I do that. First I tried the simple solution:
<a href="<?php echo base_url(); ?>application/PW1">
But access is forbidden this way. How can I do this?
Take a look at the documentation . It states that
Note: Each of your applications will need its own index.php file which calls the desired application. The index.php file can be named anything you want.
So according to the folder structure you have created, it should look something like this
system
application
----PW1
----index.php (with $application_folder = 'application/PW1';)
----PW2
----index.php (with $application_folder = 'application/PW2';)

Creating short url for multiple controller?

For my current project in codeignitor I needed to make user profile like this
http://domain.com/userid
Then I tried to add this in router.php
$route['(:any)'] = 'profile/user/$1';
Which is working fine. Now I want to make another URL for language like this
http://domain.com/es
http://domain.com/fr
As for both url uri segments are first, when I type
http://domain.com/es
I see the page of
http://domain.com/userid
I am using .htaccess file for removing index.php in codeignitor. Is there any help how can I achive this task in making shot url for multiple controller. Either with .htaccess or router.php?
Because the routes system works from the top down, if you have multiple rules that can match a url, it picks the first one. So you could do:
$route['(es|fr|en)'] = 'language/$1';
$route['(:any)'] = 'profile/user/$1';
If the first rule matches, it runs, otherwise it tests the profile rule.
You will definitely continue running into issues though with that profile rule, and it would be easier if you did something like:
$route['users/(:any)'] = 'profile/user/$1';
That way it would be more clear what the url is doing, and it will help you for when you are writing rules in the future.

Categories