Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch-all Controller/Route

Tags:

codeigniter

If I want to catch every single non existing URL that was passed to my web application and serve them a view without serving a 404, how would I do that?

Essentially I need to record the statistics based on these hits, but I need to do it by serving content and not a 404 error.

As far as I can tell from application/config/routes.php, I could use

$route['default_controller'] = 'catchall';

but I need that for my actual web application.

I could also use

$route['404_override'] = 'catchall';

but I don't want to throw 404s.

I tried using

$route['(:any)'] = '$1';

but I need to record the entire URL (e.g. any length of segments), not just the first segment.

like image 613
bafromca Avatar asked Sep 23 '11 18:09

bafromca


People also ask

What is App UseRouting ()?

UseRouting adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. UseEndpoints adds endpoint execution to the middleware pipeline. It runs the delegate associated with the selected endpoint.

How do I use MapControllerRoute?

MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); The route names give the route a logical name. The named route can be used for URL generation. Using a named route simplifies URL creation when the ordering of routes could make URL generation complicated.

What is route C#?

Routing is used to map requests to route handlers. Routes are configured when the application starts up, and can extract values from the URL that will be used for request processing.

How do I add a controller to my route?

Basic Controllers You can define a route to this controller method like so: use App\Http\Controllers\UserController; Route::get('/user/{id}', [UserController::class, 'show']);


1 Answers

Use $route['(:any)'] = 'catchall_controller'. Then in your controller you can access the URI segments using $this->uri->segment(n).

like image 167
birderic Avatar answered Jan 02 '23 06:01

birderic