Skip to content

Instantly share code, notes, and snippets.

@jlamim
Last active November 30, 2016 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlamim/95d7764264e1c1bbab17a455e9256f1b to your computer and use it in GitHub Desktop.
Save jlamim/95d7764264e1c1bbab17a455e9256f1b to your computer and use it in GitHub Desktop.
Live - CodeIgniter 4 - Rotas
<?php
//Configuração inicial
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(false);
//Estrutura base
$routes->add('journals', 'App\Blogs');
$routes->add('blog/joe', 'Blogs::users/34');
$routes->add('product/(:any)', 'Catalog::productLookup');
//Placeholders
$routes->addPlaceholder('uuid', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}');
$routes->add('users/(:uuid)', 'Users::show/$1');
//Expressões Regulares
$routes->add('login/(.+)', 'Auth::login/$1');
//Closures
$routes->add('feed', function()
{
$rss = new RSSFeeder();
return $rss->feed('general');
}
);
//Mapeamento de rotas$routes = [];
$routes['product/(:num)'] = 'Catalog::productLookupById';
$routes['product/(:alphanum)'] = 'Catalog::productLookupByName';
$collection->map($routes);
//Rotas de redirecionamento
$routes->add('users/profile', 'Users::profile', ['as' => 'profile']);
// Redireciona para uma rota nomeada
$routes->addRedirect('users/about', 'profile');
// Redireciona para uma URI
$routes->addRedirect('users/about', 'users/profile');
//Rotas agrupadas
$routes->group('admin', function($routes)
{
$routes->add('users', 'Admin\Users::index');
$routes->add('blog', 'Admin\Blog::index');
}
);
//Verbos HTTP
$routes->get('products', 'Product::feature');
$routes->post('products', 'Product::feature');
$routes->put('products/(:num)', 'Product::feature');
$routes->delete('products/(:num)', 'Product::feature');
//Rotas CLI
$routes->cli('migrate', 'App\Database::migrate');
//Definindo um namespace
$routes->add('admin/users', 'Users::index', ['namespace' => 'Admin']);
//Limitando um hostname
$collection->get('from', 'to', ['hostname' => 'accounts.example.com']);
//Limitando a um subdomínio
$routes->add('from', 'to', ['subdomain' => 'media']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment