Laravel Change URL First page Routes - laravel-5

I have an Laravel 5.4 Website with User Login.
If i enter the Url the script redirect to Login page, not "home" of course.
I want now an new Page if i enter the URL not Login Page. An Page with Login Button and after it goes to Login page. I have created an new blade where i want as first page: start.blade.php
How i can change this?
I have try it with my code in web.php but dont work, my Code:
Route::group(['middleware' => 'auth'],function(){
Route::get('logout','AuthController#Logout')->name('logout');
Route::get('/', 'HomeController#index')->name('home');
Route::get('myprofile','ProfileController#Index')->name('profile');

You have to add a new route for your page outside the group :
Route::get('start','tController#start')->name('start');
Route::group(['middleware' => 'auth'],function(){
Route::get('logout','AuthController#Logout')->name('logout');
Route::get('/', 'HomeController#index')->name('home');
Route::get('myprofile','ProfileController#Index')->name('profile');
}
And then you have to change in the Exceptions->Handler.php :
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest(route('login')); // <-- change here :)
}
You have to change return redirect()->guest(route('login')); to your new route :)
return redirect()->guest(route('start'));

Add a new route in your routes.php file with no middleware attached to it. Use this:-
Route::get('start','StartController#start')->name('start');
Now your routes.php file should looks like this :-
// Newly added route for handling pre-login calls.
Route::get('start','StartController#start')->name('start');
Route::group(['middleware' => 'auth'],function(){
Route::get('logout','AuthController#Logout')->name('logout');
Route::get('/', 'HomeController#index')->name('home');
Route::get('myprofile','ProfileController#Index')->name('profile');
});
You need to create a new controller StartController with a function named start to achieve this without disturbing current structure of code.
start function in controller:-
public function start() {
return view('< new view name here >');
}

Related

Laravel auth middleware returns route not defined

I'm new to laravel and I'm trying to secure some routes wherein only authenticated users can access it.
I've followed instructions on grouping my web routes on an auth middle ware, so I did my routes/web.php it like this...
Route::group(['middleware' => 'auth'], function () {
Route::get('/feed', [FeedController::class, 'feed']);
Route::get('/profile', [ProfileController::class, 'profile']);
});
Route::get('/', [LandingController::class, 'landing']);
and my App/Http/Middleware/Authenticate.php like this....
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('/');
}
}
but when I access these guarded routes unauthenticated, it gives me error saying
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [/] not defined.
Can someone point me on the right way here?

Laravel redirect route with a parameter from controller

I'm trying to redirect a route from a controller function after a form submit process in Laravel 5.4 as it is said in link below
https://laravel.com/docs/5.4/redirects#redirecting-named-routes
Route;
Route::group(['middleware' => ['api'], 'prefix' => 'api'], function () {
Route::post('doSomething', 'Page#doSomething');
});
Route::post('/profile', function () {
//..
});
Controller;
public function doSomething(Request $request){
return redirect()->route('profile', ['id'=>1]);
}
When I try to redirect I get this error.
InvalidArgumentException in UrlGenerator.php line 304: Route [profile]
not defined.
I have searched several times about redirection but I got similar results.
Any suggestions ? Thank you.
route() works with named routes, so name your route
Route::post('/profile', function () {
//..
})->name('profile');

Removing a route from Auth::routes()

I have these routes:
Auth::routes();
Route::get('/home', 'LibraryController#home');
Route::get('/', 'LibraryController#index');
Auth::routes() is generated by the command php artisan make::auth. But, i don't want the index page to be under auth middleware group, the third route in the above list.
Here is the controller methods. index() is for everyone and home() for authenticated users.
public function index()
{
return view('index');
}
public function home()
{
return view('home')->with('message','Logged in!');
}
the login users is redirected to home url:
protected $redirectTo = '/home';
But whenever i run the third route the login page appears. so, how can i remove that route from auth middleware group.
In your LibraryController before index where your controllers start you need to write
public function __construct()
{
$this->middleware('auth', ['except' => ['index']]);
}
Now every user will be able to go to index method without authentication
Documentation Reference https://laravel.com/docs/5.0/controllers#controller-middleware
Since Laravel 7.7 you can use excluded_middleware property eg:
Route::group([
'excluded_middleware' => ['auth'],
], function () {
Route::get('/home', 'LibraryController#home');
Route::get('/', 'LibraryController#index');
});

Protect routes with middleware Laravel

I have implemented middleware roles and permissions control in my app, but I cannot figure out why it only allows me to define one '/' route. The second one is still pointing to '/home' even though I override AuthController redirectTo variable.
My routes:
Route::group(['middleware' => 'role:user'], function()
{
Route::get('/', 'ScoresController#user');
});
Route::group(['middleware' => 'role:admin'], function()
{
Route::get('/', 'PagesController#home');
});
In any case after authentication user with user role redirecting to '/home'.
Like Simon says your second route will override the first one what you could do is load another controller wich redirects you to another page via redirect()
or write it as a route itself.
Could look like this:
Route::get('/', function(){
$user = Auth::user();
if($user->is('admin') {
return redirect('admin');
}else{
return redirect('user');
}
});
Route::get('admin', ['middleware' => 'role:admin', 'uses'=> 'PagesController#home']);
This is just one of many possibilities hope it helps you.

Redirect to Login page if ther user not logged in Laravel

I am using Laravel version 5. I have a route file for my project which contains plenty of lines. I need to put authentication like Redirect to login page if the user is not logged in. And I also want to prevent direct URL access if the user not logged in. What can I do?
And I have used below code
Route::group(array('before' => 'auth'), function(){
Route::get('/', 'HomeController#index');
});
But this prevents only for home page. I need to protect All my routes. And also suggest me how to reduce route file lines.
You can put as many routes in a Group as you like to
Route::group(array('before' => 'auth'), function(){
Route::get('/', 'HomeController#index');
Route::post('store', 'HomeController#store');
Route::get('show', 'AnotherController#index');
// ...
});
If you really need to protect all your routes, you could add
public function __construct()
{
$this->middleware('auth');
}
To your main controller class, Http/Controllers/Controller
Edit for your comment, taken from the docs, you may use
return redirect()->intended('view');
/**
* Handle an authentication attempt.
*
* #return Response
*/
public function authenticate()
{
if (Auth::attempt(['email' => $email, 'password' => $password]))
{
return redirect()->intended('dashboard');
}
}
}

Resources