Laravel, use auth and auth.basic - php

I've got a website with an 'admin dashboard'. I would like to limit access to the admin dashboard with the auth.basic filter in Laravel.
But the site itself also has an account system, and I would like to use the 'normal' auth filter for that.
Is it possible to use those two filters seperate from each other, but on the same website?

Use groups and define other routes inside them.
Route::group(['prefix'=>'admin', 'before'=>'auth.basic'], function(){
Route::get('/', function(){});
});
Route::group(['prefix'=>'account', 'before'=>'auth.account'], function(){
Route::get('/', function(){});
});

Related

How to use session data (auth) in the Laravel routes page

We have a large website with many pages. Almost all of them require the user to log in. Instead of specifying "Auth" on every single page, or on every single controller, I would like to set the routes based on if the user is logged in, like this:
// in web.php
if (Auth::isLoggedIn()) {
Route::get('/', function () { return view('pages/dashboard'); });
... lots more
}
The reason I can't do this is because Auth uses sessions, and sessions are not yet initialized in web.php, since it is done as middleware which is not run yet at this point.
I'm using Laravel 8, I believe.
Thanks.
you can group the route that need the user to be logged in, then use auth middleware
for the grouped routes:
Route::middleware(['auth'])->group(function () {
Route::get('/', function () {
//
});
Route::get('/', function () { return view('pages/dashboard'); });
});
Try using Laravel's Route middleware. Route middleware can be used to only allow authenticated users to access a given route.

Redirect All URL's With A Particular Pathname To A Certain View - LARAVEL

Okay i'm running two react projects using laravel, a website and an admin section. I want both apps rendered on separate pages because their css would clash.
So in my web.php i have this Route::view('/{path?}', 'app');, but this redirects all my routes to my app.blade.php view.
I'm justing wondering if i can have a route that redirects any route with a specific pathname, let's say: mydomainname.com/admin to my admin.blade.php. Then every other route goes to my app.blade.php.
You can use Route::prefix like this:
Route::prefix('admin')->group(function () {
Route::get('users', function () {
// Matches The "/admin/users" URL
});
});
Okay i was able to pull it off. Erkan Ozkok's answer gave me a hint.
Route::prefix('admin')->group(function () {
Route::view('/{path?}', 'admin');
});
Route::any('{query}',
function() { return view('app'); })
->where('query', '.*');

Laravel localization with Voyager admin

I'm making a Laravel website with Voyager admin and now I need to add localization to that website. Voyager includes its own translations table and I'm using it for creating content in multiple languages from backend. But in the frontend routing is not working, I'm getting NotFoundHttpException error. My routes are as following:
Route::group(['prefix' => 'fr'], function()
{
App::setLocale('fr');
Route::get('{slug}', 'PageController#show');
Route::get('posts/{slug}', 'PostsController#show');
//and so on
});
How can I fix this?
According to the documentation you can go
Route::prefix('admin')->group so
Route::prefix('fr')->group(function()
{
App::setLocale('fr');
Route::get('{slug}', 'PageController#show');
Route::get('posts/{slug}', 'PostsController#show');
//and so on
});
AND also the order is important.

In Laravel, How can I apply a filter on all routes of a website except one specific route?

In Laravel PHP Framework, How can I apply a filter on all routes/pages of the website except one specific route/page?
Update:
It will be great if there is a way other than (route groups)?
use route groups
// unsecured routes.
Route::get('/', 'UserController#getLogin');
Route::group(array('before' => 'yourFilter'), function()
{
// secured by filter `yourFilter`.
Route::controller('route1', 'XxxController');
Route::post('user/save', function() {
// content
});
Route::get('user', 'UserController#getUser');
});

laravel route filter to check user roles

I am building a restful api in laravel 4 where there are users with different types of permission. I want to restrict access to different routes depending on the user role (which is saved in the user table in db)
How would I do that? Here is what I have so far (it's not working so far).
filters.php
//allows backend api access depending on the user's role once they are logged in
Route::filter('role', function()
{
return Auth::user()->role;
});
routes.php
Route::group(array('before' => 'role'), function($role) {
if($role==1){
Route::get('customer/retrieve/{id}', 'CustomerController#retrieve_single');
Route::post('customer/create', 'CustomerController#create');
Route::put('customer/update/{id}', 'CustomerController#update');
}
});
Is it possible that I'm writing the syntax wrong for a "group filter"?
Try the following:
filters.php
Route::filter('role', function()
{
if ( Auth::user()->role !==1) {
// do something
return Redirect::to('/');
}
});
routes.php
Route::group(array('before' => 'role'), function() {
Route::get('customer/retrieve/{id}', 'CustomerController#retrieve_single');
Route::post('customer/create', 'CustomerController#create');
Route::put('customer/update/{id}', 'CustomerController#update');
});
There are different ways you could implement this, for starts Route filters accept arguments:
Route::filter('role', function($route, $request, $value)
{
//
});
Route::get('someurl', array('before' => 'role:admin', function()
{
//
}));
The above will inject admin in your Route::filter, accessible through the $value parameter.
If you need more complicated filtering you can always use a custom route filter class (check Filter Classes):
http://laravel.com/docs/routing#route-filters
You can also filter within your controller, which provides a better approach when you need to filter based on specific controllers and methods:
http://laravel.com/docs/controllers#controller-filters
Finally you can use something like Sentry2, which provides a complete RBAC solution to use within your project:
https://cartalyst.com/manual/sentry
From laravel 5.1.11 you can use the AuthServiceProvider: https://laravel.com/docs/5.1/authorization

Categories