Redirect to a specific domain specified route - laravel

Consider the following route set up:
Route::group(['domain' => 'blog.adambalan.local'], function() {
Route::get('login', 'BlogController#login');
Route::get('blogs', 'BlogController#getBlogs');
Route::post('postLogin', 'BlogController#postLogin');
});
Now consider the following, which is in the postLogin:
if (Auth::attempt($credentials)) {
Session::flash('success', "Welcome back Adam. Care to manage your blogs?");
return redirect()->route('blogs');
} else {
return redirect()->back()->withErrors(['We could not log you in. Sorry.']);
}
The issue is with: return redirect()->route('blogs');
The error is: Route [blogs] not defined. Is there a specific thing I'm suppose to do? A specific way to call domain specific routes?

You would need to name a route 'blogs'. The redirect()->route('blogs') refers to a route name not a url/path.
For a url/path you can use redirect()->to($url) or just redirect($url).
Laravel - Routing - Named Routes

Related

Add username as prefix in route URI in Laravel 9

I am building an application where users are registered and want to be redirected to their individual dashboard like this .
http://localhost/project/{username}/dashboard,
Now, its happening like
localhost/project/vendors/dashboard (here all users are accessing same URL)
but I want to make it like :
http://localhost/project/{username1}/dashboard, http://localhost/project/{username2}/dashboard
Googled lot but none of them are explained well and working.
Please assist with complete flow.
I want to declare the value of {username} globally and use it in route as prefix.
I dont want to use it before each name route. will use it as prefix and group with all vendors routes
I have made this, and its working as
localhost/project/vendors/dashboard
Route::prefix('vendors')->group(function () { Route::middleware(['auth:vendor'])->group(function () { Route::get('/dashboard', [VendorController::class, 'dashboard'])->name('vendor.dashboard'); });
});
You can specify route parameters in brackets like so {parameter}, change your code into this.
Route::get('project/{username}/dashboard', [UserDashboardController::class, 'dashboard'])
->name('user.dashboard');
In your controller you could access it like this.
class UserDashboardController
{
public function dashboard(string $username)
{
User::where('username', $username)->firstOrFail();
// something else
}
}
Seems like in your routes your are mixing vendor prefix logic in with something that in your specifications of what your urls should look like does not match. Which i think is making up some of the confusion on this case.
You can use route prefix like this
Route::prefix('{username}')->group(function () {
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', [UserController::class, 'dashboard'])->name('user.dashboard');
});
});

Laravel ui auth: Own route or customize blades for "email/verify"

I use 5 subdomains in a project and would like to change the design e.g. for the route
verification.notice
for each subdomain. I would also prefer a separate route for each one
sub1.domain.tld/email/verify
...
sub2.domain.tld/email/verify
Is there a solution for this?
Route::domain('www.domain.tld')->group(function () {
Auth::routes(['verify' => true]);
if (str_contains(url()->previous(), 'sub1.')) {
return Redirect::route('sub1.verification.notice');
} else {
//
}
});
My problem is that sub1.verification.notice is not found although the route exists.
EDIT: Change to laravel Fortify and now it works

Laravel PUT routes return 404

I'm trying to update data in the database
but it returns error 404 not found in postman
Route::put('/products/{id}','productController#update');
Just add 'Accept: application/json' header to request.
Please provide more code so we can find exactly where your problem is.
I assume you wrote the route in the api.php file and not in web.php file.
If you do so, you must enter the route as api/products/1.
You have to be aware of the route group prefix as well if you are using it. Every routes inside a group prefix will be wrapped and requires the prefix string in the beginning every time you want to access it.
For instance:
in web.php file:
Route::group(['prefix' => 'api'], function () {
Route::put('products/{id}', 'productController#update');
});
# this will require you to access the url by tyiping "api/products/1".
and in the api php file (this is the more likely for new users have to be aware):
Route::group(['prefix' => 'api'], function () {
Route::put('products/{id}', 'productController#update');
});
# this will require you to access the url by tyiping "api/api/products/1" since the api.php file will automatically wrap your routes within an api prefix.
and one more thing you need to know, if you are using a getRoutesKeyName method on your model, you should follow wether the wildcard to use id's or maybe slug based on what you type inside the method.
For instance:
public function getRoutesKeyName(){
return 'slug';
}
# this will require you to type "products/name-of-product" instead of "products/1"

Echoing the domain I'm 301 redirecting in Laravel 5.3

I'm redirecting a secondary domain in my routes:
Route::group(['domain' => 'http://olddomain.com'], function() {
Route::get('/', function() {
return Redirect::to('http://newdomain.com/', 301);
});
});
I want to be able to add an alert to a page that was redirected from olddomain.com. My solution is to use an if statement but I don't know how to check if the user has entered olddomain.com if there's a redirect happening.
Is there any way to echo olddomain.com if that's how the user is trying to access the site?
Have you tried to flash it to a session? Once you detect the first request, you should be able to add some information to the session. Once you redirect, you can check to see if this information exists in the session, and if it does, perform whatever action you need.
To start, you should always point your routes to an actual controller. It doesn't take much work, and later on it will allow you to take advantage of Laravel's route caching. Assuming you are using a Controller, your code might look like this.
public function oldRoute(){
Session::flash('old_route', \Request::url());
// Perform any additional logic
return Redirect::to('http://newdomain.com/', 301);
}
public function newRoute(){
if(!empty(Session::get('old_route'){
// You know the user came from the old route
}
// Return your view
}
Because session data are not preserved accross different domains, the simplier approach would be to pass a hashed parameter into your url before you redirect to the new domain.
On your new domain you can detect the url parameter, decrypt it and do your stuff accordingly.
Routes redirect cross-domain with hashed token parameter
// Your secret key can be anything you want
$secretKey = 'oldlaravel.dev';
Route::group(['domain' => 'oldlaravel.dev'], function() use ($secretKey) {
Route::get('/', function() use ($secretKey) {
$hashedToken = Hash::make($secretKey);
return Redirect::to("http://newlaravel.dev?token=$hashedToken", 301);
});
});
Route::group(['domain' => 'newlaravel.dev'], function() use ($secretKey) {
Route::get('/', function() use ($secretKey) {
if (Request::get('token')) {
if (Hash::check($secretKey, Request::get('token'))) {
echo 'You got here by redirect from my old domain!';
} else {
echo 'Who the hell are you?!'; //came here with an invalid token parameter
}
} else {
echo 'You got here directly!';
}
});
});

How to log all routes acess?

I build api service using laravel.
I want to log all acess to the api routes
I though somewhere in the routes.php put some code that get the requested route? any help? thanks
laravel 4
You can define a route filter first
Route::filter('log', function($route, $request, $response)
{
// log work
});
then apply the filter to your route
Route::get('api', array('before' => 'log', function()
{
return 'logged!';
}));
I think you can also get the log from the access log of your web server.

Resources