Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

routing component outside framework

Tags:

php

laravel

i have simple composer.json file:

{
    "require": {
        "illuminate/routing": "4.1.*"
    }
}

And index.php:

<?php

require_once 'vendor/autoload.php';

$router = new Illuminate\Routing\Route();

$router->get('/', function(){
   echo 'test';
});

What additionally code do you need to run routing?

like image 731
Jacek Jagiełło Avatar asked Mar 07 '14 02:03

Jacek Jagiełło


People also ask

What are the routing components?

A routing system has three parts: A RouteCollection, which contains the route definitions (instances of the Route class); A RequestContext, which has information about the request; A UrlMatcher, which performs the mapping of the path to a single route.

What is PHP routing?

Routing is what happens when an application determines which controllers and actions are executed based on the URL requested. Simply put, it is how the framework gets from http://localhost/users/list.html to the Users controller and the list() action.

Why we use routing in Laravel?

Routing in Laravel allows you to route all your application requests to their appropriate controller. The main and primary routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) along with a closure, given that it should have to be a simple and expressive way of routing.


1 Answers

Right now some of Laravel's components are not designed in a way to make them easy to use separate.

However, with some hacking I got it to work:

index.php:

<?php

require_once 'vendor/autoload.php';

$dispatcher = new Illuminate\Events\Dispatcher;
$router = new Illuminate\Routing\Router($dispatcher);

$router->get('/', function(){
   return 'test';
});

$request = Illuminate\Http\Request::createFromGlobals();
$response = $router->dispatch($request);
$response->send();

composer.json:

{
    "require": {
        "illuminate/routing": "4.1.*",
        "illuminate/events": "4.1.*"
    }
}

And you will need to setup the pretty URIs for Laravel as normal.

like image 91
smdvlpr Avatar answered Oct 07 '22 21:10

smdvlpr