@@ -406,6 +406,30 @@ public function __construct($config = 'rest')
406406 // At present the library is bundled with REST_Controller 2.5+, but will eventually be part of CodeIgniter (no citation)
407407 $ this ->load ->library ('format ' );
408408
409+ // Determine supported output formats from configiguration.
410+ $ supported_formats = $ this ->config ->item ('rest_supported_formats ' );
411+
412+ // Validate the configuration setting output formats
413+ if (empty ($ supported_formats ))
414+ {
415+ $ supported_formats = [];
416+ }
417+
418+ if (!is_array ($ supported_formats ))
419+ {
420+ $ supported_formats = [$ supported_formats ];
421+ }
422+
423+ // Add silently the default output format if it is missing.
424+ $ default_format = $ this ->_get_default_output_format ();
425+ if (!in_array ($ default_format , $ supported_formats ))
426+ {
427+ $ supported_formats [] = $ default_format ;
428+ }
429+
430+ // Now update $this->_supported_formats
431+ $ this ->_supported_formats = array_intersect_key ($ this ->_supported_formats , array_flip ($ supported_formats ));
432+
409433 // Get the language
410434 $ language = $ this ->config ->item ('rest_language ' );
411435 if ($ language === NULL )
@@ -808,6 +832,20 @@ protected function _detect_input_format()
808832 return NULL ;
809833 }
810834
835+ /**
836+ * Gets the default format from the configuration. Fallbacks to 'json'.
837+ * if the corresponding configuration option $config['rest_default_format']
838+ * is missing or is empty.
839+ *
840+ * @access protected
841+ * @return string The default supported input format
842+ */
843+ protected function _get_default_output_format ()
844+ {
845+ $ default_format = (string ) $ this ->config ->item ('rest_default_format ' );
846+ return $ default_format === '' ? 'json ' : $ default_format ;
847+ }
848+
811849 /**
812850 * Detect which format should be used to output the data
813851 *
@@ -876,7 +914,7 @@ protected function _detect_output_format()
876914 }
877915
878916 // Obtain the default format from the configuration
879- return $ this ->config -> item ( ' rest_default_format ' );
917+ return $ this ->_get_default_output_format ( );
880918 }
881919
882920 /**
0 commit comments