CodeIgniter Forums
Using a custom user agent subclass to get the segment part - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Using a custom user agent subclass to get the segment part (/showthread.php?tid=11548)



Using a custom user agent subclass to get the segment part - El Forum - 09-12-2008

[eluser]JulianM[/eluser]
I used the following subclass in order to extend a custom User_agent referrer function that provides only the segment part within the application for the referrer URL. It's very simple and is only intended to remove the site_url() part.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY User Agent Class
*
* Subclass for User Agent
*
* @subpackage  Libraries
* @category    User Agent
* @author      Julian Magnone
*/
class MY_User_agent extends CI_User_agent {

    function __construct()
    {
            parent::__construct();
    }
        
        function myreferrer()
        {
            echo substr_replace( $this->referrer(), '', 0, strlen(site_url()) );
        }
}


In my specific case, it was useful for 'nexturl' implementation. For example, in case you have two different point of entrance for a specific function. Imagine you have a list, view and edit pages. The list allows to edit an entry, the view also allows you to edit the entry. When editing, you save and then you you would like to return to the calling page.

In the edit function you can save a $nexturl page and then when saving you can redirect to the nexturl. The problem with next url is that if you plan to use Codeigniter redirect() implementation, you should pass the segment part (not only the url). Using $this->agent->myreferrer() it returns the segment part only.

In your edit function, you use:

Code:
$data['nexturl'] = $this->agent->myreferrer();

And then, in your save function you can redirect as follows:

Code:
if ($this->input->post('nexturl'))
{
    redirect($this->input->post('nexturl'));
}else{
    redirect('listing');
}

Julian Magnone