Showing posts with label Router. Show all posts
Showing posts with label Router. Show all posts

Sunday, June 18, 2017

Laravel 5.X Forward To URL | Redirect To Route | Forward Request | Mock Request | Mock HTTP Request | Dynamic HTTP Request | Laravel Dispatch Request

Laravel 5.X Forward To URL | Redirect To Route | Forward Request | Mock Request | Mock HTTP Request | Dynamic HTTP Request.

It's easy to forward a request using Laravel Route. Forwarding do for you is URL will not change but response will change. The way it is working is that it will mock a request using Request::create(...) and dispatch using current Router.

<?php
namespace App\Http\Controllers;

use Illuminate\Routing\Router;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request as HttpRequest;

class MyController extends Controller {
    public function __construct(HttpRequest $httpRequest, Router $router) {
        $this->middleware('auth', ['except' => ['action1', 'action2']]);

        $this->httpRequest = $httpRequest;
        $this->appRouter = $router;
    }

    //Route=my/need-forward (Create new request and dispatch)
    public function needForward() {
        //http://www.domain.com/my/need-forward
        $route_name = action("MyController@forwardHere");
        //http://www.domain.com/other-folder/my/need-forward
        $route_name = action("OtherFolder\\MyController@forwardHere");
        //http://www.domain.com/my/need-forward/100/Name%20Text?roll=303
        $route_name = action("MyController@forwardHere", ["id" => 100, "name" => "Name Text", "roll" => 303]);
        $request = HttpRequest::create($route_name, 'GET', array("param1" => "Param 1", "param2" => "Param 2"));
        return app()->handle($request);
    }

    //Route=my/need-forward (Forward same request to new route)
    public function needForward2() {
        $route_name = "my/forward-here";
        $request = HttpRequest::create($route_name);
        return  $this->appRouter->dispatch($request);
    }

    //Route=my/forward-here
    public function forwardHere() {
        echo "Browse [/my/need-forward] will send output of [/my/forward-here]";
    }
}


Friday, April 21, 2017

How to get complete current URL for Cakephp 3.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/