Prioritizing Laravel 4 routes - php

I have routes like this:
Route::group(array('before' => 'installed'), function() {
Route::group(array('before' => 'auth_admin', 'prefix' => 'admin'), function()
{
Route::group(array('prefix' => 'gag'), function() {
Route::get('/', 'Admin\\GagController#index');
Route::get('delete/{id}','Admin\\GagController#delete');
});
});
});
I need to prevent users from deleting content on my demo application. So I added the following piece of code before my actual routes.
if(App::environment() === 'demo')
{
Route::get('admin/gag/delete/{id}', function() {
die("You can't delete anything on demo application.");
});
}
//Actual routes are at the below.
However, it doesn't work when Route::get('delete/{id}','Admin\\GagController#delete'); is there. Somehow, Laravel ignores my if block and priorities this route. (Although if block is at the top.)
Looks like routes.php parses my if block after routes are parsed.
How can I make it so Laravel prioritizes my demo routes? I just want to restrict access to some features like this.
Ps. I don't want to add all the routes in if blocks. I just want the routes in my if block to be prioritized.

Route filters are better to do this sort of restrictions:
Route::filter('checkDemo', function()
{
if (App::environment() === 'demo')
{
return Redirect::to('home')->withMessage('You can''t delete anything on demo application.');
}
});
And set the filter to your route:
Route::group(array('prefix' => 'gag', 'before' => 'checkDemo'), function()
{
...
});
Or you can filter just that particular route:
Route::get('delete/{id}', array('before' => 'checkDemo', 'uses' => 'Admin\\GagController#delete'));

Related

Laravel 5 Auth Filter do not work

I just tried Laravel 5 after a time at 4.2..
The docs says it is possible to use 'before' => 'auth' as always, but for me it does not work.
I have no idea whats wrong, I have read the docs, search on internet but seems not to find anything.
My code looks as:
$router->group(['before' => 'auth'], function($router)
{
//
$router->get('admin', function()
{
return View::make('admin.index');
});
//
$router->get('login', function()
{
return View::make('admin.login');
});
});
Anyone can see what I doing wrong here?
In laravel5 filters are removed. Instead you can use middleware classes which are more clean.
In this blog you can read more about the middleware classes and that they're a replacement of filters.
If you want to do it with self written routes you can use this:
Route::group(['middleware' => 'auth'], function()
{
Route::get('admin', function()
{
return View::make('admin.index');
});
Route::->get('login', function()
{
return View::make('admin.login');
});
});

Laravel grouping routes with one name

i have this below route and that can work correctly
Route::get('admin/login', array('as'=>'login', function()
{
return View::make('back_end.login');
}));
app
views
back_end
layouts
index.blade.php
main.blade.php
profile.blade.php
login.blade.php
for admin i have any view for show and i want to grouping that with admin perfix. after this action and use
http://localhost/laravel/public/admin/login
http://localhost/laravel/public/admin/profile
URL i get this error:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
this is my routes:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('login', function()
{
return View::make('back_end.login');
});
Route::get('index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('profile', function()
{
return View::make('back_end.layouts.profile');
});
});
how to fix this routes. please help me
I had the same issue recently. Here is a slimmed down version of the routing that I used, including a catch all. I was routing to controllers, however you can replace that syntax with a function, the rout will be handled the same.
Route::group(array('prefix' => 'admin'), function(){
Route::get('/','AdminController#index');
Route::resource('users', 'UserController');
Route::get('settings','AdminController#settings');
/* Catch all route */
Route::any('{all}', function($uri){
return Redirect::to('admin')
->with('flash_error', "The administration page 'admin/$uri' could not be found.");
})->where('all', '.*');
});
As always, make sure to run composer dump-autoload after updating the routes. This worked successfully for me. You will only need the '/' on the relative 'base' route.
Make changes (add a preceding slash / to each routes inside the admin group) as given below:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('/login', function()
{
return View::make('back_end.login');
});
Route::get('/index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('/profile', function()
{
return View::make('back_end.layouts.profile');
});
});
It should be /login instead of login and same for each one.

Pattern filters in Laravel 4

I want to make a pattern route, to redirect users to login page when they are not logged in.
I searched but couldn't find a solution. as always Laravel's Documentation is useless!
I have this in my filter.php
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::guest('login');
});
Route::filter('auth.basic', function()
{
return Auth::basic();
});
And this route in my routes.php
Route::when('/*', 'auth' );
but It's not working. How can I do that?
Change the route declaration for login like this
Route::get( '/login', array('as' => 'login', 'uses' => 'UserController#getLogin') );
In your filters.php use
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('login');
});
So, you can use
Route::when('*', 'auth' );
You can also use
Route::group(array('before' => 'auth'), function ()
{
// Define all routes here with auth
Route::get('my_first_route', function(){ /... });
Route::post('my_second_route', 'MyController#myAction');
});

Can I group multiple domains in a routing group in Laravel?

Let's say I have the following:
Route::group(array('domain' => array('admin.example.com')), function()
{
...
});
Route::group(array('domain' => array('app.example.com')), function()
{
...
});
Route::group(array('domain' => array('dev.app.example.com')), function()
{
...
});
Is there any way to have multiple domains share a routing group? Something like:
Route::group(array('domain' => array('dev.app.example.com','app.example.com')), function()
{
...
});
Laravel does not seem to support this.
I'm not sure why I didn't think of this sooner, but I guess one solution would be to just declare the routes in a separate function as pass it to both route groups.
Route::group(array('domain' => 'admin.example.com'), function()
{
...
});
$appRoutes = function() {
Route::get('/',function(){
...
});
};
Route::group(array('domain' => 'app.example.com'), $appRoutes);
Route::group(array('domain' => 'dev.app.example.com'), $appRoutes);
I'm not sure if there is any significant performance impact to this solution.
Laravel 5.1
Route::pattern('subdomain', '(dev.app|app)');
Route::group(['domain' => '{subdomain}.example.com'], function () {
...
});
Route::pattern('subdomain', '(dev.app|app)');
Route::pattern('domain', '(example.com|example.dev)');
Route::group(['domain' => '{subdomain}.{domain}'], function () {
...
});
You can pass on the domain name as well:
Route::pattern('domain', '(domain1.develop|domain2.develop|domain.com)');
Route::group(['domain' => '{domain}'], function() {
Route::get('/', function($domain) {
return 'This is the page for ' . $domain . '!';
});
});
Just in case you need to know with which domain name the controller is called.
Tested it with Laravel 5.6.
Interested in this also! I'm trying to register a local development + production subdomain route, for the one controller action.
i.e.
# Local Dev
Route::group(array('domain' => "{subdomain}.app.dev"), function() {
Route::get('/{id}', 'SomeController#getShow');
});
# Production Server
Route::group(array('domain' => "{subdomain}.app.com"), function() {
Route::get('/{id}', 'SomeController#getShow');
});
I tried:
# Failed
Route::group(array('domain' => "{account}.app.{top_level_domain}"), function() {
Route::get('/{id}', 'SomeController#getShow');
});
But it failed.
Not a huge issue, as DesignerGuy mentioned I can just pass in a function to both routes - but it would just be more elegant if they could be grouped :)
check in laravel docs, if you main domain is myapp, in production is myapp.com and in local environment is myapp.dev try using a *
Route::group(array('domain' => '{subdomain}.myapp.*'),
function()
{
...
});
according to laravel document
in laravel 5.4+ you can use this way:
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
A better answer found on Laravel discussion on the same topic is with macros. It works great for me.
https://github.com/laravel/framework/issues/4017#issuecomment-267820459
Route::macro("domain", function(array $domains, \Closure $definition) {
foreach ($domains as $domain) {
Route::group(['domain' => $domain], $definition);
}
});
Route::domain(['foo.bar.dev', 'foo.bar'], function($route) {
// Do stuff
});
Currently you cannot. I had the same 'problem'; my fix is to cycle through your subdomains with a foreach and register the routes.
see this link. http://laravel.com/docs/routing#sub-domain-routing
Route::group(array('domain' => '{subdomain}.example.com'), function()
{
...
});
or Use this package.
https://github.com/jasonlewis/enhanced-router
It help you can set where on group routing like this.
Route::group(array('domain' => '{maindomain}'), function()
{
...
})->where('maindomain', '.+\.example\.com$');

How to use Laravel's wildcards on route group before auth?

I have this code below in my router.php
Route::group(array('before' => 'auth'), function()
{
Route::get('account/(:all?)', function() {});
Route::get('facebook/(:all?)', function() {});
});
Route::controller(Controller::detect());
It works well when the user is not logged in. But once he is successfully logged in and gets redirected to the requested page, the page is not displaying anything; just a blank page. I have tried to use :any instead of :all and it does the same thing.
Can anybody identify the problems?
Your routes are mapped to empty closures. You need to return something or map them to controllers.
Route::get('account/(:any?)', function() {
return "Hello World";
});
Route::get('account/(:any?)', function() {
return View::make('accounts.index');
});
//assuming you have an AccountController.php
Route::get('account/(:any?)', 'account#index');
//automatically route all methods of a controller
Route::controller('account');
Check out the laravel docs on routing.
Apparently, I did not find the better solution for using the group filter. The way I do it now to redirect guests to auth is this:
Route::filter('before', function()
{
$open_routes = array(
'',
'home',
'auth',
'help'
);
if(!in_array(URI::segment(1), $open_routes) && Auth::guest()) {
return Redirect::to('/auth/login');
}
});

Categories