I am beginner in Laravel. I have project in Laravel 6
I have this code in routes/admin.php:
Route::name('admin.')->group(function(){
Auth::routes(['register' => false, 'reset' => false, 'confirm' => false, 'verify' => false]);
Route::get('/', 'HomeController#index')->name('admin.home');
Route::get('/', 'HomeController#index')->name('home');
});
Route::group(['prefix' => ''], function () {
//Auth::routes(['register' => false, 'reset' => false, 'confirm' => false, 'verify' => false]);
/* Pages */
Route::get('/pages', 'PageController#index')->name('page.index');
Route::get('/pages/create', 'Admin\PageController#create')->name('page.create');
Route::post('/pages/store', 'Admin\PageController#store')->name('page.store');
Route::get('/pages/edit' . '/{id?}', 'Admin\PageController#edit')->name('page.edit');
Route::put('/pages/update', 'Admin\PageController#update')->name('page.update');
Route::delete('/pages/destroy'. '/{id?}', 'Admin\PageController#destroy')->name('page.destroy');
/* Users */
Route::get('/users', 'Admin\UserController#index')->name('user.index');
Route::get('/users/create', 'Admin\UserController#create')->name('user.create');
Route::post('/users/store', 'Admin\UserController#store')->name('user.store');
Route::get('/users/edit' . '/{id?}', 'Admin\UserController#edit')->name('user.edit');
Route::put('/users/update', 'Admin\UserController#update')->name('user.update');
Route::delete('/users/destroy'. '/{id?}', 'Admin\UserController#destroy')->name('user.destroy');
});
All from this URL need login user (admin).
In RouteServiceProvider I have:
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
protected function mapAdminRoutes()
{
Route::prefix(config('app.admin_prefix'))
->middleware('web')
->namespace($this->namespace.'\Admin')
->group(base_path('routes/admin.php'));
}
protected function mapAdminOnlyRoutes()
{
Route::middleware('web')
->namespace($this->namespace. '\Admin')
->group(base_path('routes/admin.php'));
}
How can I change my admin.php?
Now user which is no login can view my url
CODE AFTER UPDATE:
Code after update. Is this code written in an optimal way?
Route::name('admin.')->group(function(){
Auth::routes(['register' => false, 'reset' => false, 'confirm' => false, 'verify' => false]);
Route::get('/', 'HomeController#index')->name('admin.home');
Route::get('/', 'HomeController#index')->name('home');
});
Route::group(['prefix' => ''], function () {
/* Pages */
Route::get('/pages', 'PageController#index')->middleware('auth')->name('page.index');
Route::get('/pages/create', 'PageController#create')->middleware('auth')->name('page.create');
Route::post('/pages/store', 'PageController#store')->middleware('auth')->name('page.store');
Route::get('/pages/edit' . '/{id?}', 'PageController#edit')->middleware('auth')->name('page.edit');
Route::put('/pages/update', 'PageController#update')->middleware('auth')->name('page.update');
Route::delete('/pages/destroy'. '/{id?}', 'PageController#destroy')->middleware('auth')->name('page.destroy');
/* Users */
Route::get('/users', 'UserController#index')->middleware('auth')->name('user.index');
Route::get('/users/create', 'UserController#create')->middleware('auth')->name('user.create');
Route::post('/users/store', 'UserController#store')->middleware('auth')->name('user.store');
Route::get('/users/edit' . '/{id?}', 'UserController#edit')->middleware('auth')->name('user.edit');
Route::put('/users/update', 'UserController#update')->middleware('auth')->name('user.update');
Route::delete('/users/destroy'. '/{id?}', 'UserController#destroy')->middleware('auth')->name('user.destroy');
});
Let's say you want to protect Route::get('/users', 'Admin\UserController#index')->name('user.index'); route so that only users may access it, you need to do like so:
Route::get('/users', 'Admin\UserController#index')->middleware('auth')->name('user.index');
But if you need to open it to only admin and not other users, you can create your own middleware. See docs https://laravel.com/docs/7.x/middleware#defining-middleware.
Then you need to register your middleware in $routeMiddleware in App\Http\Kernel.php. For example, you register your middleware as admin. Then you would protect your route like so:
Route::get('/users', 'Admin\UserController#index')->middleware('admin')->name('user.index');
in the first step you can use the resource method to create create, store, show, edit, update and delete routes. like this:
Route::resource('shops', 'ShopController');
and for authenticated users for can use the auth middleware to your routes like these ways:
Route::get('profile', ['middleware' => 'auth', function()
{
// Only authenticated users may enter...
}]);
// With A Controller...
Route::get('profile', ['middleware' => 'auth', 'uses' => 'ProfileController#show']);
Route::group(['prefix' => 'backend', 'as' => 'backend.', 'namespace' => 'Backend', 'middleware' => ['web', 'auth], function(){})
Related
I'd like to pre check two different Route Groups by the auth:admin middleware. This works perfectly for the first Route Group inside but not for the second which is in an other Namespace.
My Routes file looks like this:
Route::group(['middleware' => ['auth:admin']], function(){
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'as' => 'admin.'], function(){
Route::resource('dashboard', 'DashboardController')->only(['index', 'create', 'store']);
});
Route::group(['prefix' => 'team/{team_id}', 'namespace' => 'Team', 'as' => 'team.'], function(){
Route::resource('dashboard', 'DashboardController')->only(['index', 'create', 'store']);
});
});
If I'm not logged in and try to go to admin/dashboard, I'm redirected to login/admin. But if I try to go to team/1/dashboard it says Error 'Trying to get property 'headers' of non-object'.
How can I get the auth:admin Middleware to work with my Team Routes too?
create a middleware
class IsAdmin
{
public function handle($request, Closure $next)
{
if (Auth::user()->permission == 'admin') {
return $next($request);
}
return redirect()->route('some.route'); // If user is not an admin.
}
}
Register in kernel.php
protected $routeMiddleware = [
....
'is.admin' => \App\Http\Middleware\IsAdmin::class,
];
So your routes:
Route::group(['middleware' => 'is.admin'], function () {
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'as' => 'admin.'], function(){
Route::resource('dashboard', 'DashboardController')->only(['index', 'create', 'store']);
});
Route::group(['prefix' => 'team/{team_id}', 'namespace' => 'Team', 'as' => 'team.'], function(){
Route::resource('dashboard', 'DashboardController')->only(['index', 'create', 'store']);
});
});
check app/Http/Controllers/Middleware/RedirectIfAuthenticated.php file and
update the code for different guard use
// app/Http/Controllers/Middleware/RedirectIfAuthenticated.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
public function handle($request, Closure $next, $guard = null)
{
if ($guard == "admin" && Auth::guard($guard)->check()) {
return redirect('/admin');
}
if ($guard == "writer" && Auth::guard($guard)->check()) {
return redirect('/writer');
}
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
What is the best and correct way to write the following routing:
Route::group(['middleware' => ['web']],function (){
Route::prefix('user')->group(function () {
//this address shows the login page
Route::any('login', 'User#login_page');
//others address that control the login action
Route::prefix('login')->group(function (){
Route::get('google', 'User#check_user_login_with_google');
Route::post('form', 'User#check_user_login_with_form');
Route::get('google-url', 'User#redirect_to_google_url');
});
//these address control the registration actions
Route::any('register','User#register');
Route::any('register/check','User#check_user_registration');
});
});
You can do it all in one shot:
Route::group(['prefix' => 'user', 'as' => 'user.', 'middleware' => ['web']], function() {
Route::any('login', 'User#login_page');
...
})
You can use all these in a separate associated array and assign this array to the group here is how to do that .
Route::group(['prefix' => 'user', 'as' => 'user.', 'middleware' => ['web']], function() {
Route::any('login', 'User#login_page');
...
})
Hope this may help you
Trying to play with Laravel today for the first time. I am getting the following error when I attempt to visit:
InvalidArgumentException
Route [dashboard] not defined.
routes/web.php
Route::get('/', ['as' => '/', 'uses' => 'LoginController#getLogin']);
Route::post('/login', ['as' => 'login', 'uses' => 'LoginController#postLogin']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController#getLogout']);
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController#dashboard']);
});
LoginController.php
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $username = 'username';
protected $redirectTo = '/';
protected $guard = 'web';
public function getLogin()
{
if (Auth::guard('web'))
{
return redirect()->route('dashboard');
}
return view('login');
}
public function postLogin(Request $request)
{
$auth = Auth::guard('web')->attempt([
'username' => $request->username,
'password' => $request->password,
'active' => 1]);
if ($auth)
{
return redirect()->route('dashboard');
}
return redirect()->route('/');
}
public function getLogout()
{
Auth::guard('web')->logout();
return redirect()->route('/');
}
}
as like name(). You should use one of the two :
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', 'DashboardController#dashboard')->name('dashboard');
});
Or
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', [
'as' => 'dashboard',
'uses' => 'DashboardController#dashboard']);
});
After, you clear route cache with php artisan route:clear
Final, you can use php artisan route:list to lists all routes and the action bind
Try this:
Route::get('/dashboard','DashboardController#dashboard')->name('dashboard');
When you use as it names the route , so you have add two name dashbbaorddashboard because you use as and name
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', ['uses' => 'DashboardController#dashboard'])->name('dashboard');
});
This will work
I want the route to bypass authentication filter after successfully login. So I use Redirect::intended. But it makes problem. Here is my code
In LoginController#doLogin
if (Auth::attempt($credentials)) {
return Redirect::intended("home.index");
}
In Routes
Route::group(array('before' => 'auth'), function() {
Route::resource('home', 'HomeController');
Route::get('/', 'HomeController');
});
My routes
If I put the the home resource route without the auth filter, then it will work. (Not redirect to login route.).That code is given below
Route::resource('home', 'HomeController');
Route::group(array('before' => 'auth'), function() {
Route::get('/', 'HomeController');
}); /* No Problem with this code */
But I want to work with auth filter.I'm using laravel 4.
Please help me...
Currently I use this:
in filter.php
Route::filter('sentry', function() {
if (!Sentry::check())
{
return Redirect::route('authLogin');
}
});
so in route.php
Route::get('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login'));
Route::post('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login'));
Route::group(array('before' => 'sentry'), function() {
Route::get('user/', array('as' => 'user', 'uses' => 'UserController#index'));
Route::get('user/index', array('as' => 'userIndex', 'uses' => 'UserController#index'));
});
I preffer named routes.
I'm trying to route the index page to a different location if logged in however even though my authentication system works, it's not redirecting to where I expected i.e. getLogged, instead it always redirects to getIndex whether I am logged in or not.
Route::filter('auth', function()
{
if (!Sentry::check()) return Redirect::to('/');
});
Route::group(array('before' => 'auth'), function() {
Route::get('/', array('uses' => 'MyController#getLogged'));
});
Route::get('/', array('before' => 'detectLang', 'uses' => 'MyController#getIndex'));
I tested to make sure my auth works by changing
Route::group(array('before' => 'auth'), function() {
Route::get('/', array('uses' => 'MyController#getLogged'));
});
to
Route::group(array('before' => 'auth'), function() {
Route::get('/dash', array('uses' => 'MyController#getLogged'));
});
and that properly behaves that I can only access /dash when I am logged in so why is my index route not working?
You're declaring the same route twice, it won't work. To achieve this functionality, instead of adding a auth filter, add a guest one that, instead of checking if the user is not connected, will check if it is. Something like this:
Route::filter('guest', function () {
if (Sentry::check()) return Redirect::route('logged');
});
Then, setup your routes, something along these lines:
Route::get('/', array(
'as' => 'home',
'uses' => 'MyController#getIndex',
'before' => 'guest'
));
Route::get('/logged', array(
'as' => 'logged',
'uses' => 'MyController#getLogged',
'before' => 'auth|detectLang'
));
Note: The as key gives a name to your route, so you can use it on Redirect::route or URL::route methods.