url shortener and expander
1.0.3
Perpustakaan URL Shortener/Expander.
$ composer require emrahtoy/url-shortener $ urlShortener = new UrlCompressor Shortener ( $ connection , $ config );
// returns true or false depending on success and error.
$ result = $ urlShortener -> shorten ( $ url );
// responseJson is a tool to return json with proper headers. This function also redirect with 301 code if you send true as secondary parameter.
// You can send "donotredirect" in order to prevent redirection even it is set "true"
UrlCompressor Common:: responseJson ( $ urlShortener -> getResult (), false ); $ urlExpander = new UrlCompressor Expander ( $ connection , $ config );
// returns true or false depending on success and error.
$ result = $ urlExpander -> expand ( $ shortened_code );
// responseJson is a tool to return json with proper headers. This function also redirect with 301 code if you send true as secondary parameter.
// You can send "donotredirect" in order to prevent redirection even it is set "true"
UrlCompressor Common:: responseJson ( $ urlExpander -> getResult (),( isset ( $ _REQUEST [ ' donotredirect ' ]))? false : true ); $ config = [
' CheckUrl ' => false , // check url before shortening or after expand
' ShortUrlTableName ' => ' shortenedurls ' , // Database table name where shortened url codes are stored
' CustomShortUrlTableName ' => ' customshortenedurls ' , // Database table name where your legacy shortened codes are stored
' TrackTableName ' => ' track ' , // Database table name where the visit/redirect counts are stored
' DefaultLocation ' => '' , // where to redirect if expanded url could not find
' Track ' => false // Determines if visit/redirects will be stored
];Anda dapat menggunakan pustaka cache yang didukung oleh Doctrine Cache.
Contoh ini menggunakan klien predis dengan redis
$ urlShortener = new UrlCompressor Shortener ( $ connection , $ config );
$ urlExpander = new UrlCompressor Expander ( $ connection , $ config );
// using Predis Client with Doctrine Cache
try {
$ cacheConfig = [
' scheme ' => ' tcp ' ,
' host ' => ' 127.0.0.1 ' ,
' port ' => 6379 ,
' password ' => ' supersecretauthentication ' // if you have set authentication on redis
];
//lets create redis client
$ cacheClient = new Predis Client ( $ cacheConfig );
// and try to connect
$ cacheClient -> connect ();
// we can encapsulate cache cilent with doctrine cache if any exception not fired
$ cache = new Doctrine Common Cache PredisCache ( $ cacheClient );
// set cache provider for url shortener service
$ urlShortener -> setCache ( $ cache );
// set cache provider for url expander service
$ urlExpander -> setCache ( $ cache );
} catch ( Exception $ e ){
$ result = new UrlCompressor Result ();
$ result -> error ( $ e -> getMessage ());
UrlCompressor Common:: responseJson ( $ result -> result ());
die ();
}