Create component or application component

There is many ways to create an FTP connection. Connection should be global for the application, so we ca, create it as an application component. It should be specifc to a controller, so we can create a "simple"component.

Create an application component

Edit protected/config/main.php and add a new component :

// main.php
return array(
	// [...]
	'components'=>array(
		// [...]
		'ftp' => array(
			'class' => 'ext.GFtp.GFtpApplicationComponent',
			'connectionString' => 'ftp://user:pass@host:21',
			'timeout' => 120,
			'passive' => false
		)
	),
	// [...]
);

Application component should be access anywhere in application using :

$gftp = Yii::app()->ftp;
// connection, login and passive are automatically done

Create an simple component

There are two ways to create a component : instanciate directly the GFtpComponent class, or using Yii component creation facilities

  • Instanciate GFtpComponent class (first way) :
    // create component directly
    Yii::import('ext.GFtp.GFtpComponent')
    
    /*
     * 1st parameter : Connection URL
     * 2nd parameter : Connection timeout
     * 3rd parameter : Set passive mode
     */
    $gftp = new GFtpComponent('ftp://user:pass@host:21', 120, false);
    
    // call to connect and login is not mandatory. Each FTP function connect and log 
    // onto the server if it is not already connected.
    $gftp->connect();
    $gftp->login();
    
    
  • Instanciate GFtpComponent class (second way) :
    // create component directly
    Yii::import('ext.GFtp.GFtpComponent')
    
    $gftp = new GFtpComponent();
    $gftp->connectionString = 'ftp://user:pass@host:21';
    $gftp->timeout = 120
    
    // call to connect and login is not mandatory. Each FTP function connect and log 
    // onto the server if it is not already connected.
    $gftp->connect();
    $gftp->login();
    $gftp->pasv(false);
    
  • Using Yii creation component facilities :
    // create component directly
    Yii::import('ext.GFtp.GFtpComponent')
    
    $gftp = Yii::createComponent('ext.GFtp.GFtpComponent', array(
    	'connectionString' => 'ftp://user:pass@host:21', 
    	'timeout' => 120, 
    	'passive' => false)
    );
    
    // call to connect and login is not mandatory. Each FTP function connect and log 
    // onto the server if it is not already connected.
    $gftp->connect();
    $gftp->login();
    
    
TOC