Function inside route group [laravel-passport] - php

i have some function that check something and give back in some cases "exit();".
i want to use it inside Route::group.
how can i do it right without it impact all the other routes? thanks!
Route::group(['middleware' => ['auth:api']], function() {
myFunction (); //this function can give back: exit();
Route::get('/test', 'Api\Test#test');
});

Turn your function into middleware: https://laravel.com/docs/5.8/middleware
Group the routes that must be affected by your check, and leave out the routes that don't.
Route::group(['middleware' => ['auth:api']], function() {
Route::group(['middleware' => ['MyMiddleware']], function() {
Route::get('/check-me', 'Api\Test#test1');
});
Route::get('/dont-check-me', 'Api\Test#test2');
});

Related

Laravel 8 prefix to current route

Route::middleware(['auth', 'admin'])->prefix('admin')->group(function () {
Route::get('/', [App\Http\Controllers\Admin\IndexController::class, 'index']);
});
and when I go to this url http://127.0.0.1:8000/admin/
it shows error: The requested resource /admin/ was not found on this server.
Try using below approach:
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function(){
Route::get('/', [App\Http\Controllers\Admin\IndexController::class, 'index']);
});
Try this code:
Route::middleware(['auth', 'admin'])->prefix('admin')->group(function () {
Route::get('', 'IndexController#index');
});
Please look inside your Public folder. You probably created a folder called 'admin'. Rename that into something else or remove it if you're not using it.

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');
});
});

Prioritizing Laravel 4 routes

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'));

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