• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unlikely...

#1
Brick 
Very unlikely that it is possible, but..

There are many methods in my controllers that have a specific param: $webinar_id
Most of the time it is the only parameter (but not always).
Can I somehow CHECK it - ownership issue - BEFORE method executes, without altering methods themselves? Rolleyes

I had hopes with 'post_controller_constructor' hook.. i could even read this param via uri->segment(3) but.. can't distinguish "webinar_id" form other params... sad.
Reply

#2
If the parameter is being passed through a URI segment, the only thing you can really go by is the URI. If you're dealing with a relatively limited number of controllers/methods and the webinar_id is in a consistent location in the URI, it's fairly easy to just check the necessary segments and determine whether the value in segment 3 should be a webinar_id (for example, if the first and second segments match the required values). In the end, though, it probably requires a level of knowledge about the controllers/methods being called that really isn't appropriate for a hook.

Whether there might be some other way to do this without altering the methods themselves really depends on the existing code, but it is unlikely.
Reply

#3
All in all we decided to go "altering methods" way, inserting check function call wherever necessary...

Looks like it's a downside of URI-segment routing: when there is no "&id=" you can't be sure that first param is actually an "id".
Reply

#4
(12-30-2015, 01:25 AM)Rashid Wrote: All in all we decided to go "altering methods" way, inserting check function call wherever necessary...

Looks like it's a downside of URI-segment routing: when there is no "&id=" you can't be sure that first param is actually an "id".

If the URI segments are well defined and consistent, you can make some assumptions that if URI segment 1 is within a set of values and URI segment 2 is within a set of values, than URI segment 3 must be an ID in order for it to be a valid URI. However, that can be cumbersome for some situations, and may not be flexible enough for some sites.

Even in the best cases, you have to assume that someone may be supplying invalid data, since the URI can be input by the user, but that's the case regardless of whether you use URI-segment routing.

You could also use the URI segments themselves as you would a query string, which is supported by the uri_to_assoc() and assoc_to_uri() methods in the URI library, but that would require modification to your controller methods or your routing.
Reply

#5
Quote:There are many methods in my controllers that have a specific param: $webinar_id
Most of the time it is the only parameter (but not always).

define different routes for the different conditions. so for example when webinar_id is not the only parameter -
then create a different route to pick up the other params.

otherwise you can check and validate anything you want in the controller constructor, and then redirect or whatever you need before the controller method is called.
Reply

#6
I use a key:value pair in my URI to distinguish required variables.
It looks like this: http://www.yourwebsite.com/controller/me...ar_id:1234

For this I have extended the core URI class and have a method that searches for key:value pairs in each available uri segment.
If a segment is a key:value pair, it is removed from the segments array, and I rebuild the URI segments array.

The found pairs are stored in an array params.
So your webinar_id would be stored as:
PHP Code:
$this->params['webinar_id'] = 1234

Below are the methods that I use.

Find the key:value pairs
PHP Code:
/**
 * Method to extract key:value params from the url
 */
private function _set_params() {
 
   if(count($this->segments) > 0) {
 
       foreach($this->segments as $key => $segment) {
 
           ifstrpos($segment':') ) {
 
               list($key_name$value) = explode(':'$segment);
 
               if(is_numeric($value)) {
 
                   $this->params[$key_name] = (int)$value;
 
               } else {
 
                   $this->params[$key_name] = $value;
 
               }
 
               $this->_unset($key);
 
           }
 
       }
 
   }


Reset the URI segment array

PHP Code:
/**
* Method to reindex the segments array after any unset()
*/
private function _unset($key) {
   // Check if the key exists in the segments array
   ifarray_key_exists($key$this->segments) ) {
       // Unset the passed key
       unset($this->segments[$key]);

       // re-index the segments array
       $nkey 1;
       $segments $this->segments;
       $this->segments = array();
       foreach($segments as $segment) {
           $this->segments[$nkey] = $segment;
           $nkey++;
       }
    }


Get all available parameters

PHP Code:
/**
* Return the set parameters
*/
public function get_params() {
   return $this->params;


Get a specific parameter

PHP Code:
/**
 * Return a set param
 */
public function get_param($key) {
 
   ifarray_key_exists($key$this->params) ) {
 
       return $this->params[$key];
 
   }
 
   return NULL;


To get the value of webinar_id in your controllers method, or any method in any class just do:
PHP Code:
$webinar_id $this->uri->get_param('webinar_id'); 

All of the above methods are placed in the extended core URI class MY_URI

Hope this can help you out
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.