URL routing patterns for frontend pages in PHP - php

Im creating a MySQL database driven PHP (W)CMS application which follows the MVC pattern. First take a look at the framework:
The MVC framework handles the request and decides what to load/call based on the URL, like: http://domain.com/user/details/121 will load and instantiate a User controller object, and calls its details(121) method with the userid passed as a parameter, and then instantiate a User_Model and "ask" it for the detailed data of the user with the 121 userid, and at last display the result with a View. This is the basic concept of an MVC architecture. Nothing particular, everything is clear at this point.
Whereas this will be a CMS, I want to handle a Page model. A user with the nesessary permissions (mostly admin and/or root) can perform basic CRUD operations and other stuff on a page, for example:
I can create a page with the:
tile = 'About us' (this will be displayed as a headline of the page or the title of the browser tab like eg.: HTML title and h1 tags)
URL denomination = '*about_us*' (this will be the URI endpoint, like: http://domain.com/about_us)
reference name = 'Who we are' (This is the text displayed in the menubar)
page content = 'lorem ipsum...' (The actual content of the page...by a WYSIWYG html text editor)
and much more options like structuring the pages, to assign sub pages under a parent page, or making a page startpage (which means if I set 'About us' as a start page, then http://domain.com will automaticall load that page content)...
Or I can modify these properties, even I can delete a page...etc.
The MVC framework makes no difference between handling a frontend and a backend call.
For example we have some requests:
http://domain.com/user/details/121
http://domain.com/about_us
http://domain.com/our_products/1255
The first will load a backend controller as I detailed before,
but the others will load a frontend content.
When the Bootstrap loads the appropriate controller/action we look for the actual controller file, in the example above :
/controllers/Users.php
/controllers/About_us.php
/controllers/Our_products.php
The first can be loaded because that is a 'static' controller written before, but the About_us and Our_products are not existing controllers so If it is impossible to load the controller, the bootstrap searches the database if is there a page with the same URL denimination (like: about_us, our_products). If there is, we load a common FrontEndController and display the requested page data, if there isn't, display a 404 error.
I do this because I want the bootstrap to handle all requests the same way, but I dont want to every frontend URL compulsorily contain the FrontEndController (e.g.:http://domain.com/FrontEndController/our_products/1255). So this is how I hide it from the user, so the URL can remain more user friendly. My question is: Is this a good practice? Or are there any other proper ways to do this?

The MVC framework handles the request and decides what to load/call based on the URL
What you would normally is have is some sort of Router and Dispatcher class. The router would accept the the user/details/121, parse it and return a Route.
$route = $router->route( $request->getUri() );
The router could hold config values like the allowed space character in URI's, default allowed characters etc.
You can also add custom routes to the router
$router->addRoutes($routes);
The custom routes can be a simple associative array
$routes['requested-uri'] = 'custom-route'
In the example above you said when they visit the root of the website you want them to actually see the About Us page so that could be done like this:
$router->addRoutes([
'' => 'about-us
]);
Meaning when the URI is ''(blank) then go to the 'about-us' route. It shouldn't do a redirect, just transparently load up a different route while keeping the URI in the clients web browser the same.
Routing can obviously be more complex, using route objects added to a route collection for more advanced custom routing with more control. Some frameworks use annotations and all sort of different ways to achieve flexible routing.
The dispatcher could then accept the route returned from the router and dispatch it. That means verifying if the requested route actually exists i.e does the controller file exist and the requested method in the controller exist.
$view = $dispatcher->dispatch($route);
Inside the Dispatcher::Dispatch() method:
// Check if the controller file exists.
// Instantiate the controller file, preferably using a controller factory.
// Check if the controller method exists.
// Call the controller method
call_user_func_array([$controller, $route->getMethod()], $route->getParams());
$view = $controller->getView();
$action = $route->getAction();
// Call the view method.
if( method_exists($view, $action) ) {
$view->$action();
}
return $view;
I find the following a very easy to understand way of dealing with controller methods/actions. Let's say you have a login controller, the user sends a GET request to it first and a POST request to it when sending the login details in the form.
public function getIndex() { }
public function postIndex() {
$username = $this->request->post('username');
$password = $this->request->post('password');
}
The get and post in front of the method name is the request type, this prevents you having to do something like this
public function index() {
if( $this->request->getType() === 'POST' ) {
$username = $this->request->post('username');
$password = $this->request->post('password');
}
}
It also gives you more control over authorisation(if you do it at the routing layer) because you can easily allow a user to send a GET request to the controller but deny them access to sending a POST request.
Each controller has a one to one relationship with a view. The view get's injected into the controller on construction, preferably using a controller factory.
What would happen when you send a GET request http://domain.com/user/details/121 is the router would break up the URI and turn it into a route targeting the User controller, the getDetails() method with the parameter 121, the dispatcher checks if the controller and method exist, it then calls the method supplying the user ID as an argument, the controller sets the user ID in the view. Below is the User controller.
public function getDetails($userId) {
$this->getView()->setUserId( (int)$userId );
}
The view then has a method called details(). The same name as the method called in the controller, just without the request type in front of it.
The dispatcher then calls the details() method of the view which then fetches the required data.
Setting the title of the page is done in the view, as it is for presentation purposes only.
Part of the view that is related to the User controller
public function details() {
// Fetch the user by using the previously set user ID from the controller.
// If he doesn't exist set an error template, set the response code to 404,
// or redirect. Do whatever you want really.
$this->setTitle('User Details');
// Build template objects, bind the fetched user data to main template.
}
How you implement the setTitle method and all over view related stuff is up to you.
The view sends the response back to the client, whether it is HTML content, JSON, XML, or any other content type.
For example your application lets you search for users and export them to a Microsoft Excel Workbook file(.xlsx) and prompt the user to download it.
The view would:
Fetch the users
Generate the file
Set the HTTP response headers like Content-Type
Send the response

Related

using routes in Codeigniter to get to one of two controllers

I have two controllers Configure.php and Users.php. In my routes.cfg I have:
$route['default_controller'] = 'Users/login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['registration'] = 'registration';
$route['login'] = 'login';
$route['subit_backend']['GET']='subit_backend/register';
$route['save_userinput']='Users/save_userinput';
When a user brings up the website the following page comes up in the browser bar https://www.stantiation.com/sub_crud/Users/login/ which is perfect.
The problem is that if a user has forgotten their password I am having trouble routing them to method where they can create a new one. I have the user send an email to themselves where I have placed a "code" that will allow them to update their password. The email has a link to this form:
https://www.stantiation.com/sub_crud/Users/resetPassword?fp_code=492c8bbd3841xxx8201f3a01d77fd.
I also have a view file called view/Users/resetPassword.php which is form that allows the user to enter a new password. That pops up fine. This is the post method of the form.
When the user presses submit, I get https://www.stantiation.com/sub_crud/Users/save_userinput in the toolbar and a 404 error, because there is no save_userinput.php file. I am trying to get the save_userinput() method in the Users.php controller to run, not save_userinput.php in the view/Users/directory. I agree with the 404 because that file doesn't exist.
How can I specify in a form that I want the method in controller Users, not the file view/Users/save_userinput.php? That is why I put the last $route in but that doesn't seem to help.
It seems that you might be understanding the CodeIgniter URL scheme. Read about it HERE.
Basically, it boils down to http:doman.tld/controller/function[/var1 ...[/varN]]
So, after the domain you have multiple segments that are interpreted like so:
The first segment represents the controller that should be invoked.
The second segment represents the class function, or method, that should be called.
The third, and any additional segments, represent any variables that will be passed to the controller.
So the URL https://www.stantiation.com/sub_crud/Users/save_userinput would go to the controller Users (which appears to be in the folder of /application/controllers/sub_crud) and call the controller method save_userinput. It is not looking for a file named save_userinput.php.
The 404 could be because the controller is not in a sub-folder or because some other file the controller tries to load, i.e. a "view" file, cannot be found.
It's hard to offer better advice without seeing the html for the form and knowing exactly how you have your file layout structured.
(Side note: I avoid putting controllers in sub-folders because it messes with the URL "look" and IMO, in terms of "controllers", it's not that hard to track what-is-what.)
You really only need routes when you want to override CodeIgniter's segment-based approach to URLs. With that in mind, it appears (based on a general lack of understanding about your app) that the following "routes" don't make sense.
$route['registration'] = 'registration';
$route['login'] = 'login';
$route['subit_backend']['GET']='subit_backend/register';
$route['save_userinput']='Users/save_userinput';

CodeIgniter - Select controller based on database

I'm building a simple CMS using Code Igniter version 3.0.0
The site's URLs are all customizable by the user and so do not follow the standard MVC structure of /controller/method/parameter-1/parameter-2/. Instead, all frontend traffic gets directed to PublicController's index method. This method searches the database for the current URL to return the correct page, and also the page type. Each page type corresponds to a controller.
How do I call that controller from the PublicController without doing a redirect?
I can't use the redirect() method because that would change the URL in the browser window and cause an un-need additional page request.
if you look at the url /about/who-we-are/
about is the controller and who-we-are is a function in the controller that loads one or more views.
The same for /locations/stores/
the functions stores in the controller locations.
read the documentation and it will be easy to understand.
http://www.codeigniter.com/user_guide/overview/mvc.html
I am pretty sure that configuring a route is your answer:
// routes.php
$route['(:any)'] = "PublicController/index/$1";
// PublicController.php
public function index()
{
var_dump(func_get_args());
}

CodeIgniter building a CMS with different page types

I've just started to get into MVC with PHP and have had a good mess about with the likes of CodeIgniter and CakePHP. I'm interested to find out what people's approaches to the following would be:
Normally when I have built a website with a CMS in the past I have approached it by having a core URI table in my database. Each URI is unique and represents a page on my website (e.g. www.example.com/about would reference a record in my URI table with 'about' as the unique URI). The URI table also contains a 'type' column which tells the system what type of page it is (e.g. splash, basic, gallery or whatever). Each 'type' has a corresponding table in my database with all the data for records of that type in them (e.g. I would have tables: basic, gallery and splash). The type also tells the system which template/pagehandler to load which in turn does what it needs to do for each page type.
So if you go to www.example.com/about, my system looks in my URI table for a record with URI 'about', finds it's type to be 'basic' so it loads the basic template/pagehandler which uses the basic table in my database to load and render the page. In the CMS I follow a similar approach, I'll have add/edit forms for all of the different types in my page manager.
I was wondering how you would approach this using an MVC framework such as CodeIgniter? I essentially want to have different controllers for each type of page on both the front and back. However, when someone get's to my site, they will end up on a URI with a single level so I need to check the type of the page and pass off to the correct controller. Is there a way you would recommend of checking the type of each page and then loading the relevant controller to do the rest of the work?
My approach eventually was to extend the _parse_routes() method of the Router class to check the database for any records matching the current uri and set the request with the corresponding value from the database.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
function __construct() {
parent::__construct();
}
function _parse_routes() {
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$routes_table_exists = $db->query("SHOW TABLES LIKE 'routes';");
if ($routes_table_exists->num_rows > 0) {
$uri_routes = $db->get_where('routes', array('uri' => $this->uri->uri_string()));
if ($uri_routes->num_rows > 0) {
$row = $uri_routes->result()[0];
if (isset($row->request)) {
return $this->_set_request(explode('/', $row->request));
}
}
}
parent::_parse_routes();
}
}
Whether or not it's the best approach to take it seems to work so far.
Usually its a combination of Routes and naming your controllers. so for example you have an About page, and you don't need a separate About controller. Lets say you have a general Pages controller, and then a view($page) method to retrieve and show the page.
example.com/about
$route['about'] = "pages/view/about";
if you just have a few pages there are advantages to hard coding the routes - it protects your database. but otherwise taking an example from the tutorial
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
this does the same thing but now it will take example.com/anything can go here
Versus something like a contact page - where you probably want to have a separate controller called Contact, because you will need to validate the contact form, add it to a database, email it, show a response, show the form again if did not validate, etc So then you can just do a simple link to show the contact form: example.com/contact
the contact form submits to: example.com/contact/submit
more about Routes
http://ellislab.com/codeigniter/user-guide/general/routing.html
and definitely look at the tutorial it will give you more examples about routes
http://ellislab.com/codeigniter/user-guide/tutorial/index.html

CodeIgniter: some doubts about HMVC and Views

I've just discovered HMVC Modular Extension for CodeIgniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home and it seems perfect for my needs but I have some question.
Let's say I have two controllers:
Site which is the main controller and is used to show site's pages and may call methods of Users controller for example to show a form
User controller is used to authenticate users, to show login/sign up forms...
Now I have these questions:
If the user access the User controller directly (mysite.com/user/method) I want to show a full page while if i load a method of User from within the Site controller I want to show only a form (for example), is this possible?
What happens to view of a module loaded from another module: is the view shown automatically or i need to show it manually and how does the view behave?
If you method is being called via Modules::run()
There is a third optional parameter lets you change the behavior of
the function so that it returns data as a string rather than sending
it to your browser.
Eg:
//put underscore in front to prevent uri access to this method.
public function _module1()
{
$this->load->view('partial_view', array('some data'=>'some data'), TRUE)
}
call it inside your SITE view easily
Modules::run('User/_module1')
// should show whatever is in partial_view ie: a form
//an alternative is to pass in any params if the method requires them
Modules::run('User/_module1', $param)

How do I dynamically change a controller in Zend Framework?

I'm halfway through a CMS where the URL is an SEO friendly name based on a page title. There is a need for one section to use a specific controller. So for example:
test.com/page1 (uses index controller)
test.com/page2 (uses index controller)
test.com/page3 (uses different controller)
test.com/page4 (uses index controller)
I could add a route that says "page3" will use the "different" controller, but the users of the CMS need to be able to change the name and seo of the URL, so where it is currently "page3" it could be changed later which would break my Routing rule.
What's the best way (either front controller plug in or other) to grab the request and pull the controller to be used from the DB (sql would be like "SELECT controller FROM menu WHERE seo = 'page3'"), then set that as the controller before Zend sets the controller to be used?
Any help or insight is greatly appreciated.
You will have to create a controller plugin and set the module/controller/action on the request object.
Then in the predispatch() you can do something like this:
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$request->setModuleName($this->_getModule());
$request->setControllerName($this->_getController());
$request->setActionName($this->_getAction());
}
And then you can create methods __getModule(), _getController(), _getAction() that will examine the $_SERVER['REQUEST_URI'] and your DB and set the appropriate module/controller/action.

Categories