Showing posts with label url. Show all posts
Showing posts with label url. Show all posts

Thursday 18 July 2019

How to get complete current URL for Cakephp 3.x , 2.x

Below are few steps described to get URL inside Cakephp 3.x controller as well as view file. You can get full URL and base URL and current URL inside Controller.

The very first thing is to include Router in your Controller as below:

use Cake\Routing\Router;

public function urls()
{
    echo "<div style=''>";
    echo "Current URL=" . Router::url(null, true);
    echo "<BR>Current URL=" . Router::url(null, false);
    echo "<BR>Current URL=" . $this->request->getUri();
    echo "<BR>Current URL=" . $this->request->getUri()->getPath();
    echo "<BR>Current URL=" . $this->request->getRequestTarget();
    echo "<BR>Full URL=" . Router::url("/", true);
    echo "<BR>Base URL=" . Router::url("/", false);
    die("</div>");
}
And output would like this:

Current URL=http://localhost/cake/pages/urls
Current URL=/cake/pages/urls
Current URL=http://localhost/pages/urls?id=20&name=Pritom
Current URL=/pages/urls
Current URL=/pages/urls?id=20&name=Pritom
Full URL=http://localhost/cake/
Base URL=/cake/

Thursday 27 November 2014

To find url given string and replace with anchor tag in php

<?php
$str = "Hi I am using google.com going http://google.com and 
where in https://google.com ahcinkg http://google.co.in else http://google.co";
$ss = "";
$sd = explode(" ",$str);
//print_r($sd);
$c=count($sd)."
"; $i=0; while($c>$i){ $regex = "((https?|ftp)\:\/\/)?"; // SCHEME $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass $regex .= "([a-z0-9-.]*)\.([a-z]{2,4})"; // Host or IP $regex .= "(\:[0-9]{2,5})?"; // Port $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor // $regexp = '/^[a-zA-Z0-9][a-zA-Z0-9\-\_]+[a-zA-Z0-9]$/'; if (true == preg_match("/^$regex$/", $sd[$i])) { if(!in_array("http", parse_url($sd[$i])) && !in_array("https", parse_url($sd[$i]))) { $s1 ="http://".$sd[$i]; echo $ss ="$sd[$i]  "; }elseif(in_array("http", parse_url($sd[$i])) || in_array("https", parse_url($sd[$i]))) { echo $ss ="$sd[$i]  "; } }else{ echo $sd[$i]."  "; } $i++; } ?>

Thursday 20 November 2014

Save image from ulr using JAVA

package com.cakes;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class SaveImageFromUrl {
 public static void main(String[] args) throws Exception {
  String imageUrl = "http://www.avajava.com/images/avajavalogo.jpg";
  String destinationFile = "image.jpg";
  saveImage(imageUrl, destinationFile);
 }
 public static void saveImage(String imageUrl, String destinationFile) throws IOException {
  URL url = new URL(imageUrl);
  InputStream is = url.openStream();
  OutputStream os = new FileOutputStream(destinationFile);
  byte[] b = new byte[2048];
  int length;
  while ((length = is.read(b)) != -1) {
   os.write(b, 0, length);
  }
  is.close();
  os.close();
 }
}

Save image from url using php

<?php
$url = "http://www.technotrigger.com/wp-content/uploads/2014/01/house-in-green-field.jpg";
$content = file_get_contents($url);
file_put_contents('flower.jpg', $content);
?>
<img src="flower.jpg">

Tuesday 19 August 2014