I just need to know how i can set the login page as default page of my site.
Route::get('/', function () {
return view('welcome');
});
I need something like this:
Route::get('/', function () {
return view('auth.login');
});
It's possible?
Thanks
Yes, it's possible, just put this route before all other routes and this will work.
If you're using standard Laravel 5.2 auth system, you can create this route and use it before all other routes:
Route::get('/', 'Auth\AuthController#getLogin');
Related
I'm working on a project where for each user there will be separate subdomain for example my website name is xyz.com then for user it will be user.xyz.com, for that im trying to do subdomain routing but its not working.
Below are the routes for main domain
Route::get('/', function () {
return redirect('login');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Below are routes for subdomain
Route::domain('user.xyz.com')->group(function () {
Route::get('/posts', function () {
return 'Second subdomain landing page';
});
});
Any expert please look into this.
Suggestions are welcome.
I have a suggestion... why not pass the domain as a parameter the way you would an unknown, article id for example /article/{id}?
Try this:
Route::domain('{user}.xyz.com')->group(function () {
Route::get('/posts', [PostsController::class, 'view'])->name('posts');
});
In our PostsController output the results...
class PostsController {
public function view ($user){
dd($user) //this will output the current user's subdomain name
}
}
Let me know if it works out for you.
Try to add a domain line in RouteServiceProvider.
Route::middleware("web")->domain('Domain Name')->namespace($this->namespace)->group(base_path("routes/web.php"));
I was working in laravel 8.x, I have developed API to register, but when i test using postman also in browser the url [1]: http://127.0.0.1:8000/api/register always returns 404 not found message.
below is my api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::group(['prefix' => 'v1'], function () {
Route::post('/login', 'UsersController#login');
Route::post('/register', 'UsersController#register');
Route::get('/logout', 'UsersController#logout')->middleware('auth:api');
});
can you please help?
Since you already put a route group "v1", all your routes must have that prefix, so just api/register wont work because that route doesn't exist inside your api.php, so infront of your routes just use
http://127.0.0.1:8000/api/v1/register
http://127.0.0.1:8000/api/v1/login
http://127.0.0.1:8000/api/v1/logout
Your routes looking fine.
I think you are forgetting to add http://127.0.0.1:8000/api/**v1**/register so please try with it once.
I need to disable all routes for guests in Laravel except '/' and 'login' pages.
Does that possible to implement it routes/web.php ?
Yes. In your routes/web.php file, make sure to define your protected routes under the auth middleware group.
routes/web.php
Route::get('/', function() {
// / route
});
Route::get('/login', function() {
// login page
});
Route::middleware(['auth'])->group(function () {
// define your routes here
// they'll be protected
});
Official documentation
Since Laravel 7.7 you can use excluded_middleware property eg:
Route::group([
'excluded_middleware' => ['auth'],
], function () {
Route::get('/', 'HomeController#index');
...
});
I've developed an app for my homework and It's base on Vue.js, Laravel, base on tutorials, my problem is base on tutorial I've learned, I've wrote this app and now i can't access any route except my app.
I want to create a about page, and when i add route to route it's going to my default page of spa app, which, I've did it base on tutorials to prevent people to type nonsense in URL like url.com/asdasdqwe, so how to add exception to the line that's preventing me to access other routes?
Here's the code:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
// I know this line make my app to force it to don't access other routes like bottom('/about')
// Route::get('{path}',"HomeController#index")->where( 'path', '([A-z\d-\/_.]+)?' );
// Because of top code i can't access other views that's using ('/etcroute')
Route::get('/about', function () {
return view('welcome');
});
So when i remove {path} line i'll get some problem in SPA app. so I'm looking for a way to add exception or force it let these routes work with {Path} line.
Welcome to StackOverflow :)
To resolve this, simply place your '/about' route above the one with the regex match.
Laravel processes routes in order that they are listed, so your '/about' route is not being seen because the other one is matching it first.
For example:
Auth::routes();
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return view('welcome');
});
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
Route::get('{path}', 'HomeController#index')->where('path', '([A-z\d-\/_.]+)?');
Route::get('dashboard/{any}', [HomeController::class, 'index'])->where(['any' => '.*']);
Change your routings sort like this:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
// Because of top code i can't access other views that's using ('/etcroute')
Route::get('/about', function () {
return view('welcome');
});
Route::get('{path}',"HomeController#index")->where( 'path', '([A-z\d-\/_.]+)?' );
Laravel routing begins from the top of web.php files and checks the routings one by one.
So you have to add regex at the end of your static routes.
i am new to laravel,
i have code in my controller's __construct like
if(Auth::check())
{
return View::make('view_page');
}
return Redirect::route('login')->withInput()->with('errmessage', 'Please Login to access restricted area.');
its working fine, but what i wants is. its really annoying to put these coding in each and every controller, so i wish to put this Verify Auth and redirect to login page in one place, may be in router.php or filters.php.
I have read some posts in forum as well as in stackoverflow, and added code in filters.php like below but that's too not working.
Route::filter('auth', function() {
if (Auth::guest())
return Redirect::guest('login');
});
Please help me to resolve this issue.
Laravel 5.4
Use the built in auth middleware.
Route::group(['middleware' => ['auth']], function() {
// your routes
});
For a single route:
Route::get('profile', function () {
// Only authenticated users may enter...
})->middleware('auth');
Laravel docs
Laravel 4 (original answer)
That's already built in to laravel. See the auth filter in filters.php. Just add the before filter to your routes. Preferably use a group and wrap that around your protected routes:
Route::group(array('before' => 'auth'), function(){
// your routes
Route::get('/', 'HomeController#index');
});
Or for a single route:
Route::get('/', array('before' => 'auth', 'uses' => 'HomeController#index'));
To change the redirect URL or send messages along, simply edit the filter in filters.php to your liking.
To avoid code repetition, You can use it in middleware. If you are using the Laravel build in Auth, You can directly use the auth middleware as given,
Route::group(['middleware' => ['auth']], function() {
// define your route, route groups here
});
or, for a single route,
Route::get('profile', function () {
})->middleware('auth');
If you are building your own, custom Authentication system. You should use the middleware which will check the user is authenticated or not. To create custom middleware, run php artisan make:middleware Middelware_Name_Here and register the newly created middleware.
It's absolutely correct what other people have replied.
This solution is for Laravel 5.4
But just in case, if you have more than one middleware applying to routes, make sure 'auth' middleware comes in the end and not at the start.
Like this:
Route::prefix('/admin')->group(function () {
Route::group(['middleware' => 'CheckUser', 'middleware' => 'auth'], function(){
});
});
Route::middleware(['auth'])->group(function () {
Route::get('dashboard','BackendController#dashboard')->name('dashboard');
});
This entry in the web.php route will take the user [who is not logged in] to the login page if (s)he tries to access a 'protected' URL, "dashboard" in this case.