@@ -157,15 +157,22 @@ public function run()
157
157
//--------------------------------------------------------------------
158
158
Hooks::trigger ('pre_controller ' );
159
159
160
- $ this ->startController ();
160
+ try
161
+ {
162
+ $ this ->startController ();
161
163
162
- //--------------------------------------------------------------------
163
- // Is there a "post_controller" hook?
164
- //--------------------------------------------------------------------
165
- Hooks::trigger ('post_controller ' );
164
+ //--------------------------------------------------------------------
165
+ // Is there a "post_controller" hook?
166
+ //--------------------------------------------------------------------
167
+ Hooks::trigger ('post_controller ' );
166
168
167
- $ this ->gatherOutput ();
168
- $ this ->sendResponse ();
169
+ $ this ->gatherOutput ();
170
+ $ this ->sendResponse ();
171
+ }
172
+ catch (PageNotFoundException $ e )
173
+ {
174
+ $ this ->display404errors ($ e );
175
+ }
169
176
170
177
//--------------------------------------------------------------------
171
178
// Is there a post-system hook?
@@ -300,11 +307,15 @@ protected function loadComposerAutoloader()
300
307
*/
301
308
protected function getRequestObject ()
302
309
{
303
- $ this ->request = is_cli ()
304
- ? Services::clirequest ($ this ->config , true )
305
- : Services::request ($ this ->config , true );
306
-
307
- $ this ->request ->setProtocolVersion ($ _SERVER ['SERVER_PROTOCOL ' ]);
310
+ if (is_cli ())
311
+ {
312
+ $ this ->request = Services::clirequest ($ this ->config , true );
313
+ }
314
+ else
315
+ {
316
+ $ this ->request = Services::request ($ this ->config , true );
317
+ $ this ->request ->setProtocolVersion ($ _SERVER ['SERVER_PROTOCOL ' ]);
318
+ }
308
319
}
309
320
310
321
//--------------------------------------------------------------------
@@ -316,7 +327,10 @@ protected function getRequestObject()
316
327
protected function getResponseObject ()
317
328
{
318
329
$ this ->response = Services::response ($ this ->config , true );
319
- $ this ->response ->setProtocolVersion ($ this ->request ->getProtocolVersion ());
330
+ if ( ! is_cli ())
331
+ {
332
+ $ this ->response ->setProtocolVersion ($ this ->request ->getProtocolVersion ());
333
+ }
320
334
321
335
// Assume success until proven otherwise.
322
336
$ this ->response ->setStatusCode (200 );
@@ -415,8 +429,6 @@ protected function startController()
415
429
$ this ->benchmark ->start ('controller ' );
416
430
$ this ->benchmark ->start ('controller_constructor ' );
417
431
418
- $ e404 = false ;
419
-
420
432
// Is it routed to a Closure?
421
433
if (is_callable ($ this ->controller ))
422
434
{
@@ -426,92 +438,100 @@ protected function startController()
426
438
{
427
439
if (empty ($ this ->controller ))
428
440
{
429
- $ e404 = true ;
441
+ throw new PageNotFoundException ( ' Controller is empty. ' ) ;
430
442
}
431
443
else
432
444
{
433
445
// Try to autoload the class
434
446
if ( ! class_exists ($ this ->controller , true ) || $ this ->method [0 ] === '_ ' )
435
447
{
436
- $ e404 = true ;
448
+ throw new PageNotFoundException ( ' Controller or its method is not found. ' ) ;
437
449
}
438
450
else if ( ! method_exists ($ this ->controller , '_remap ' ) &&
439
451
! is_callable ([$ this ->controller , $ this ->method ], false )
440
452
)
441
453
{
442
- $ e404 = true ;
454
+ throw new PageNotFoundException ( ' Controller method is not found. ' ) ;
443
455
}
444
456
}
457
+ }
445
458
446
- // Is there a 404 Override available?
447
- if ($ e404 && $ override = $ this ->router ->get404Override ())
448
- {
449
- if ($ override instanceof \Closure)
450
- {
451
- echo $ override ();
452
- }
453
- else if (is_array ($ override ))
454
- {
455
- $ this ->controller = $ override [0 ];
456
- $ this ->method = $ override [1 ];
459
+ $ this ->createControllerAndRun ();
460
+ }
457
461
458
- unset($ override );
459
- }
462
+ protected function createControllerAndRun ()
463
+ {
464
+ $ class = new $ this ->controller ($ this ->request , $ this ->response );
460
465
461
- $ e404 = false ;
462
- }
466
+ $ this ->benchmark ->stop ('controller_constructor ' );
463
467
464
- // Display 404 Errors
465
- if ( $ e404 )
466
- {
467
- $ this -> response -> setStatusCode ( 404 );
468
+ //--------------------------------------------------------------------
469
+ // Is there a "post_controller_constructor" hook?
470
+ //--------------------------------------------------------------------
471
+ Hooks:: trigger ( ' post_controller_constructor ' );
468
472
469
- if (ob_get_level () > 0 )
470
- {
471
- ob_end_flush ();
472
- }
473
- ob_start ();
473
+ if (method_exists ($ class , '_remap ' ))
474
+ {
475
+ $ class ->_remap ($ this ->method , ...$ this ->router ->params ());
476
+ }
477
+ else
478
+ {
479
+ $ class ->{$ this ->method }(...$ this ->router ->params ());
480
+ }
474
481
475
- // Show the 404 error page
476
- if (is_cli ())
477
- {
478
- require APPPATH .'Views/errors/cli/error_404.php ' ;
479
- }
480
- else
481
- {
482
- require APPPATH .'Views/errors/html/error_404.php ' ;
483
- }
482
+ $ this ->benchmark ->stop ('controller ' );
483
+ }
484
484
485
- $ buffer = ob_get_contents ();
486
- ob_end_clean ();
485
+ protected function display404errors (PageNotFoundException $ e )
486
+ {
487
+ // Is there a 404 Override available?
488
+ if ($ override = $ this ->router ->get404Override ())
489
+ {
490
+ if ($ override instanceof \Closure)
491
+ {
492
+ echo $ override ();
493
+ }
494
+ else if (is_array ($ override ))
495
+ {
496
+ $ this ->controller = $ override [0 ];
497
+ $ this ->method = $ override [1 ];
487
498
488
- echo $ buffer ;
489
- exit (EXIT_UNKNOWN_FILE ); // Unknown file
499
+ unset($ override );
490
500
}
491
501
492
- if ( ! $ e404 && ! isset ($ override ))
493
- {
494
- $ class = new $ this ->controller ($ this ->request , $ this ->response );
502
+ $ this ->createControllerAndRun ();
503
+ $ this ->gatherOutput ();
504
+ $ this ->sendResponse ();
505
+ return ;
506
+ }
495
507
496
- $ this ->benchmark ->stop ('controller_constructor ' );
508
+ // Display 404 Errors
509
+ $ this ->response ->setStatusCode (404 );
497
510
498
- //--------------------------------------------------------------------
499
- // Is there a "post_controller_constructor" hook?
500
- //--------------------------------------------------------------------
501
- Hooks::trigger ('post_controller_constructor ' );
511
+ if (ob_get_level () > 0 )
512
+ {
513
+ ob_end_flush ();
514
+ }
515
+ ob_start ();
502
516
503
- if (method_exists ($ class , '_remap ' ))
504
- {
505
- $ class ->_remap ($ this ->method , ...$ this ->router ->params ());
506
- }
507
- else
508
- {
509
- $ class ->{$ this ->method }(...$ this ->router ->params ());
510
- }
511
- }
517
+ $ heading = 'Page Not Found ' ;
518
+ $ message = $ e ->getMessage ();
519
+
520
+ // Show the 404 error page
521
+ if (is_cli ())
522
+ {
523
+ require APPPATH .'Views/errors/cli/error_404.php ' ;
524
+ }
525
+ else
526
+ {
527
+ require APPPATH .'Views/errors/html/error_404.php ' ;
512
528
}
513
529
514
- $ this ->benchmark ->stop ('controller ' );
530
+ $ buffer = ob_get_contents ();
531
+ ob_end_clean ();
532
+
533
+ echo $ buffer ;
534
+ exit (EXIT_UNKNOWN_FILE ); // Unknown file
515
535
}
516
536
517
537
//--------------------------------------------------------------------
@@ -532,7 +552,7 @@ protected function gatherOutput()
532
552
//--------------------------------------------------------------------
533
553
// Display the Debug Toolbar?
534
554
//--------------------------------------------------------------------
535
- if (ENVIRONMENT != 'production ' && $ this ->config ->toolbarEnabled )
555
+ if ( ! is_cli () && ENVIRONMENT != 'production ' && $ this ->config ->toolbarEnabled )
536
556
{
537
557
$ toolbar = Services::toolbar ($ this ->config );
538
558
$ this ->output .= $ toolbar ->run ($ this ->startTime , $ totalTime ,
0 commit comments