CodeIgniter - hidden controller

I am creating a CMS based on CodeIgniter. It stores "views" and its data in a database and, if necessary, collects the necessary ones. As you might have guessed - I can’t create a physical controller and an appropriate view for each page.

I realized that the routes come in handy, since I would prefer not to use the controller visible in the URL. Poorly explained: I am looking for a way to reassign all requests that do not end on a physically existing controller to a user one without it appearing in the URL. This controller, of course, handle 404 errors, etc.

Bad: .com/handler/actual-view/)Good: (.com/actual-view/)(there is no actual view controller, or it will be shown instead)

I added a route 404_overridethat points to handler/. Now I'm looking for a way to find out the requested view (i.e., the .com/actual-viewactual view is what I'm looking for).

I tried

$route['404_override/(:any)'] = 'handler/$1';

and the like, which completely removes the 404 redefinition.

+3
source share
3 answers

My solution, after some advice from the wonderful CodeIgniters forum and the excellent StackOverflow members, began to send all 404 errors to my user controller, where I guarantee that it is a real 404 (without view controllers or controllers). Later in the controller, I collect the rest of the information that I need from the database URI string:

//Route
$route['404_override'] = 'start/handler';

//Controller
function handler($path = false) {

   //Gather the URI from the URL-helper
   $uri_string = uri_string();

   //Ensure we only get the desired view and not its arguments
   if(stripos($uri_string, "/") !== false) {
      //Split and gather the first piece
      $pieces = explode("/", $uri_string);
      $desired_view = $pieces[0];
   } else {
      $desired_view = $uri_string;
   }

   //Check if there any view under this alias
   if($this->site->is_custom_view($desired_view)) {

      //There is: ensure that the view has something to show
      if(!$this->site->view_has_data($desired_view)) {
         //No data to show, throw an error message
         show_custom_error('no_view_data');
      } else {
         //Found the views data: show it
      }

   } else {
      //No view to show, lets go with 404
      show_custom_404();
   }
}
0
source

You would be better off expanding your base router or controller.

, - , CI.

0

route.php,

$routes["(:any)"] = "specific controller path";

if I give an example:

$route['u/(:any)/account'] = "user_profile/account/$1";
$route['u/(:any)/settings'] = "user_profile/settings/$1";
$route['u/(:any)/messages'] = "user_profile/messages/$1";
$route['u/(:any)'] = "user_profile/index/$1";

as shown here, I am diverting all the URLs to the user profile after the first three have failed to catch it.

0
source

All Articles