Codeigniter: Index page creation - php

My folder structure is as following
admin
__master
_______address_book
_______Users
_______Product
_______etc
__operations
_______register_orders
_______payments
_______etc
I have created controllers for address_book,users,products,register_orders, payments etc to reduce the complexity of each controller.
Now, how to handle index page request for www.abc.com/admin ?
I have created Admin controller in /controllers directory then other links like
www.abc.com/admin/master/address_book will not work.
How to handle both requests?
I would also like to know is there any way to handle each index page requests
eg:
www.abc.com/admin/
www.abc.com/admin/master/
www.abc.com/admin/operations/

To access each request with url like-
www.abc.com/admin/
www.abc.com/admin/master/
www.abc.com/admin/operations/
you have to use codeigniter's routing. And for routing there are a config file under aplication/config folder named routes.php. Add all your routes in this file. Suppose you want to access this url-
www.abc.com/admin/operations/
then you have to create a new route for this in the route file, like-
$route['admin/operations'] = 'admin/operations';
where in $route array index you have to mention what will be the url and the value against this index you have to mention the controller's path and also you can mention the controller's function name which will be invoked (for index function there no need for mentioning).

Related

Routing in CodeIgniter for (:any)

I'm trying to make my CodeIgniter application work similarly to WordPress.
I want to be able to make these kind of URLs:
http://www.example.com/my-post-example
http://www.example.com/new-headline-here
http://www.example.com/i-love-stackoverflow
My routing:
$route['(:any)'] = "core/index/$1";
Which will call my Core controller and pass the page name into the index function.
I then lookup in my database for the page name and display the page to the user. So far so good.
However, there will be times when I want to call another controller. For example:
http://www.example.com/admin/edit_page/3
http://www.example.com/admin/settings
Now I assume my route will just grab all these rules and send them into my Core controller. Is there a way to make an exception for certain pages? Or is it a good idea to do this check inside my Core controller.
For example,
if ($page not in DB) {
// Call controller/method
}
This seems a little redundant since I just want CodeIgniter to handle this.
The routing rule you using it is OK for your purpose.
If you use http://www.example.com/admin/edit_page/3 this link it will send you admin controller and edit_page method.It will not use routes any rule.
However you will get one problem if your link looks like this
http://www.example.com/my-post-example/test
It will try to go my-post-example controller and test method.
Again http://www.example.com/admin will use routes any rule, means it will redirect your to core controller instead of admin/index. In that case your url should be http://www.example.com/admin/index
Finally If you call your other link with controller/method name it will be OK using your any rule

Does CodeIgniter always associate a controller to a segment of a URI?

This is my first attempt to work/learn CodeIgniter. However, I'm struggling in understanding the "C".
1) Does CodeIgniter always associate a controller to a segment of a URI?
2) What are the best practices to work with controllers? I mean, how can I avoid dumping all my methods in a single controller? Can I split a controller in several files without creating unnecessary URI.
1.Yes controller always associate to segment of a URI. If your controller is under some directory like
controllers
search ---------------------directory inside controller
search ------------------controller
stock_search -------------------method
then it will add whole path in the uri segment e.g :basepath.'search/search/stock_search/';
But you can route it your custom path using routes.php
$route['search'] = 'search/search/stock_search/';
2.You can create different controllers (name should be different) with the different methods or you can say you can split controller methods in different files and customise their url accordingly in routes.php and can create parent controller to use methods in any controller by extending through it.
If you want to get something in codeigniter then codeigniter send the request to a controller. URI must have a controller if no controller in uri then the reguest is goes to default controller which is tell in application/config/routes.php in this code $route['default_controller'] = 'welcome';
And will not be able to split a controller in several files without creating more than one URI.
Controller is associated to url segments.
Url used in Codeigniter is as follows: http://example.com/index.php/projname/controller/method/params.
If you dont specify controller in uri, default controller is called specified in routes.php $route['default_controller'] = 'welcome';

In Zend, How Can I Route Controller-Omitted URLs To The Index Controller

In order to have user-friendly URLs in my application, I want all URLs that call actions in the index controller to omit the controller name. For example:
/user/edit => /user/index/edit
The "index" part of the URL is unfriendly. But, I would like other controllers in the module to work as expected, such as:
/user/article/publish (does not route to /user/index/article/publish)
I am using a config file (/config/route.ini) which is loaded into the bootstrap of Zend 1.12, which basically uses Zend_Controller_Router_Route, like so:
routes.index.route = :module/:action
routes.index.defaults.controller = index
routes.index.defaults.action = index
My stumbling block is that I could not make the routes work with Zend_Controller_Router_Route. These routes should work when the page is not found. If there is an existing action in an existing controller that the URL points to, it makes sense to go there instead.
Final thought: is this even possible within Zend? Maybe I should go with the .htaccess and use mod_rewrite?

How to check if Controller already exists when already configured route using Codeigniter?

On my website, I am loading the content dynamically from database like this
e.g mysite.com/about-us
for this, there is an enrtry in database, so it will load the content for 'about-us' & print it using "page" controller only.
for this what I have done is, I have added below configuration in routes.php
$route[':any'] = "page";
but lets say if I already have controller named "about-us" and I want to load that & not the one from database, how can I do that?
A smooth solution would be to use the error/missing_page controller and point it in the config/routes.php.
Then it would automaticly pick all existing controllers first, and then that controller.
You can also call show_404() if you don't find a record in the database.
This allows you to create new controllers without having to point all of them in the route file.
Read about 404 override here
you need to add this
$route['about-us'] = "aboutus";
$route['about-us/(:any)'] = "aboutus/$1";
before
$route[':any'] = "page";
as the CI route is not greedy, it will not check for the page controller after it finds the about-us controller.

ZF controllers redirecting to index controller

I had set up a zend framework project.My index controller working fine. But when i tried to access other controllers, i m getting content of index controller only.
For example when i access user/signup, in url i m getting same user/signup, but it taking actions from index controller only.
check your pluggings. search setController(), setAction() methods in your project. When you use that methods they never change URL. But show content for that.

Categories