How do I give route name to a closure route in Lumen? - php

Hi I have the following Lumen Route
$router->get('/end', function (\Illuminate\Http\Request $request) use ($router) {
$controller = $router->app->make('App\Http\Controllers\Legacy\EndLegacyController');
return $controller->index($request);
});
I am trying to give it a route name so that I can use redirect to redirect to this route like redirect()->route('name_of_route')
so far I have tried
})->namedRoute['end'] = '/end'; // No Effect
})->name('end') //Undefined function
but it didn't work
here's a list of present routes
Edit
Because of my certain requirement, I can't use ['as' => 'end', 'uses'=> 'ControllerName#Action']

you can use the following syntax: $router->get('/end', ['as'=>'name_here', function()]);

Related

Laravel routing without appending any prefix

I have a path in Laravel it is like subdomain.mydomain.com/admin/login
I am trying to call
subdomain.mydomain.com and need to get the login page straight.
Currently, it's not working
This is the function I am using in routerserviceprovider.php
protected function mapAdminRoutes()
{
Route::middleware('subdomain.mydomain.com')
->prefix('admin')
->namespace($this->namespace)
->group(base_path('routes/admin.php'));
}
and in admin.php there is a resource group shows like this:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
//Login Routes...
Route::view('login','admin.login');
});
can anyone help with this?
Add following route
Route::get('/',function(){ return view('login.index'); })->name('admin.login');
i hope it helps

Laravel which route group does current route belong to

I am working with Laravel 5.5 and want to find out if the current route belongs to the auth:web middleware group. Is there a way to get this?
My routes:
/***********************************************************************************************************************
* Guest routes
**********************************************************************************************************************/
Route::group([
'middleware' => ['guest']
], function () {
Route::get("login", "Auth\LoginController#showLoginForm")->name("user.login");
Route::post("login", "Auth\LoginController#login")->name("user.do-login");
..................
});
/***********************************************************************************************************************
* All routes that require AUTH
**********************************************************************************************************************/
Route::group([
'middleware' => ['auth:user']
], function () {
..............
});
Something like below. Note I want the route group not the auth for the user:
$request()->currentRoute()->isGuest();
Is this possible? I cannot seem to find this in the Laravel docs.
I am trying to achieve executing a popup modal on all guest route pages without having to check and maintain a list of routes to test against. Thanks.
You can modify the handle method in the guest middleware class \App\Http\Middleware\RedirectIfAuthenticated, something like:
public function handle($request, Closure $next, $guard = null)
{
// share a token to all views for this middleware:
View::share('is_guest_route', true); // <-- this line is added
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
Make sure to also add:
use Illuminate\Support\Facades\View;
in the use-section of the file.
Now, in every view (blade file), you can do:
#if (isset($is_guest_route))
// do whatever is needed for guest routes
#endif
Fixed it with workaround. Added web. or app. prefix to all route names and now I search for this.
#if(substr(\Request::route()->getName(), 0, 4) !== 'web.')
#include('org.popupmsg.modal-pop')
#endif
with now the routes having changed to: web.user.login

How to Map new route to specific old route in Laravel?

I have following route group for admin panel
Route::prefix('admin')->group(function (){
.
.
.}
I want to wrap this route to a new route e.g asda12asda
so that old behavior :
/admin/users
is changed to :
/asda12asda/users
not allowing old route. I don't want to change it internally from the system and want to find some efficient Laravel way to achieve it.
Redirect the old route to a new route
Route::prefix('admin')->group(function (){
Route::any('login', function () {
// Redirect to new route
redirect()->route('new route');
});
});
by creating a new route and mapping it accordingly
Route::prefix('asda12asda')->group(function () {
Route::any('login', function () {
// Do whatever you were about to do
})->name('new route');
});
If you are using Laravel 5.4 then you can add a new route file. Assume your route name is
adsp.php then add it to RouteServiceProvider.php like this.
protected function mapApiRoutes()
{
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/adsp.php.php');
});
}

more than one guard in route

I use laravel framework I want to use more than one guard in my route like :
Route::group([ 'middleware' => 'jwt.auth', 'guard' => ['biker','customer','operator']], function () {}
I have a script in AuthServiceProvider.php like below in boot section:
$this->app['router']->matched(function (\Illuminate\Routing\Events\RouteMatched $event) {
$route = $event->route;
if (!array_has($route->getAction(), 'guard')) {
return;
}
$routeGuard = array_get($route->getAction(), 'guard');
$this->app['auth']->resolveUsersUsing(function ($guard = null) use ($routeGuard) {
return $this->app['auth']->guard($routeGuard)->user();
});
$this->app['auth']->setDefaultDriver($routeGuard);
});
That work with just one guard in route like 'guard'=>'biker'
So how change that code in AuthServiceProvider.php to work with more than one gaurd in route
I know this is an old question but I just went through this issue and figured out how to solve it by myself. This might be useful for someone else. The solution is very simple, you just have to specify each guard after the name of your middleware separated by commas like this:
Route::group(['middleware' => ['auth:biker,customer,operator'], function() {
// ...
});
The guards are then sent to \Illuminate\Auth\Middleware\Authenticate function authenticate(array $guards) which checks every guard provided in the array.
This works for Laravel 5.4.
Also works for Laravel 6.0.

Laravel auth check for all pages

I have created the Authentication, and its working perfectly. But there is some problem in checking the inner pages. For example,
Route::get('/', array('before' => 'auth' , 'do'=> function(){
return View::make('home.index');
}));
The index page is only visible for logged in users. But whenever I have go to the inner pages, for example example.com/products. The products page can be visible without log in.
Here is my solution.
/**
* Groups of routes that needs authentication to access.
*/
Route::group(array('before' => 'auth'), function()
{
Route::get('user/logout', array(
'uses' => 'UserController#doLogout',
));
Route::get('/', function() {
return Redirect::to('dashboard');
});
Route::get('dashboard', array(
'uses' => 'DashboardController#showIndex',
));
// More Routes
});
// Here Routes that don't need Auth.
There are several ways of applying filters for many routes.
Putting rotues into Route::group() or if you using controllers add the filter there, add it in the Base_Controller so it will be applied to all. You can also use filter patterns and use a regex which applies the filter to all except a few you don't want to.
Documentation
Route filters: http://laravel.com/docs/routing#route-filters
Example to the pattern filter, as the others are basicly in the docs. This one could be the fastest but also the most problematic because of the problematic way of registering a regex in this function (the * is actually converted into (.*)).
Route::filter('pattern: ^(?!login)*', 'auth');
This will apply auth to any route except example.com/login.
Route::group(['middleware' => ['auth']], function()
{
Route::get('list', 'EventsController#index');
});
Read more on the documentation page:
https://laravel.com/docs/5.2/routing#route-groups
There may be a better way but I take a whitelist approach. Everything is blocked from public except for what the pages I put in this array.
// config/application.php
return array(
'safe' => array(
'/',
'card/form_confirm',
'record/form_create',
'card/form_viewer',
'user/login',
'user/quick_login',
'user/register',
'info/how_it_works',
'info/pricing',
'info/faq',
'info/our_story',
'invite/accept',
'user/terms',
'user/privacy',
'email/send_email_queue',
'user/manual_login',
'checkin/',
'checkin/activate',
'system/list',
),
// routes.php
Route::filter('before', function()
{
// Maintenance mode
if(0) return Response::error( '503' );
/*
Secures parts of the application
from public viewing.
*/
$location = URI::segment(1) . '/' . URI::segment(2);
if(Auth::guest() && !in_array( $location, Config::get('application.safe')))
return Redirect::to( 'user/login' );
});
this code working fine with me
Auth::routes();
Route::group(['middleware' => 'auth'], function () {
// Authentication Routes...
Route::get('/', 'HomeController#index')->name('home');
});
The same problem can be solved using a BaseController to extends all Controller have must logged user.
Example:
class SomeController extends BaseController
{
public function index() { return view('some.index');}
}
just add a __construct() method to BaseController
class BaseController extends Controller
{
protected $redirectTo = '/myIndex'; // Redirect after successfull login
public function __construct()
{
$this->middleware('auth'); // force all controllers extending this to pass auth
}
}
More info here
Just check if user is logged in in your views.
Or restrict all controller (if you use it)
Or check Route Groups, and give a filter to whole group of routes: http://laravel.com/docs/routing#groups
Route::filter('pattern: /*', array('name' => 'auth', function()
{
return View::make('home.index');
}));
It worked for me . take a look at it.
Route::when('*', 'auth.basic');
Route::get('api/getactorinfo/{actorname}', array('uses' =>'ActorController#getActorInfo'));
Route::get('api/getmovieinfo/{moviename}', array('uses' =>'MovieController#getMovieInfo'));
Route::put('api/addactor/{actorname}', array('uses' =>'ActorController#putActor'));
Route::put('api/addmovie/{moviename}/{movieyear}', array('uses' =>'MovieController#putMovie'));
Route::delete('api/deleteactor/{id}', array('uses' =>'ActorController#deleteActor'));
Route::delete('api/deletemovie/{id}', array('uses' =>'MovieController#deleteMovie'));

Categories