Coder Social home page Coder Social logo

phprouter's Introduction

PHP Router

Installation

Install using composer or include Router.php. For cleaner urls use the following .htaccess file:

<IfModule mod_rewrite.c>
	Options +FollowSymLinks
	RewriteEngine On

	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Example

Registering a route that responds to "GET /":

Route::get('/', function()
{
	return "Hello World!";
});

Registering a route that is valid for any HTTP verb (GET, POST, PUT, and DELETE):

Route::any('/', function()
{
	return "Hello World!";
});

Registering routes for other request methods:

Route::post('user', function()
{
	//
});

Route::put('user/(:num)', function($id)
{
	//
});

Route::delete('user/(:num)', function($id)
{
	//
});

Registering a single URI for multiple HTTP verbs:

Route::register(array('GET', 'POST'), $uri, function()
{
	//
});

Registering a route that is only valid for HTTPS requests:

Route::secure('GET', '/', function()
{
	//
});

Wildcards

Forcing a URI segment to be any digit:

Route::get('user/(:num)', function($id)
{
	//
});

Allowing a URI segment to be any alpha-numeric string:

Route::get('post/(:any)', function($title)
{
	//
});

Catching the remaining URI without limitations:

Route::get('files/(:all)', function($path)
{
	//
});

Allowing a URI segment to be optional:

Route::get('page/(:any?)', function($page = 'index')
{
	//
});

Controller Routing

Controllers provide another way to manage your application logic. It is important to be aware that all routes must be explicitly defined, including routes to controllers. This means that controller methods that have not been exposed through route registration cannot be accessed. It's possible to automatically expose all methods within a controller using controller route registration.

Registering the "home" controller with the Router:

Route::controller('home');

Registering several controllers with the router:

Route::controller(array('dashboard.panel', 'admin'));

This convention is similar to that employed by CodeIgniter and other popular frameworks, where the first segment is the controller name, the second is the method, and the remaining segments are passed to the method as arguments. If no method segment is present, the "index" method will be used.

This routing convention may not be desirable for every situation, so you may also explicitly route URIs to controller actions using a simple, intuitive syntax.

Registering a route that points to a controller action:

Route::get('welcome', 'home@index');

Extras

Get your site base URL:

Router::base();

Get the current URI:

Router::uri();

phprouter's People

Contributors

marlon33 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.