I am trying to get a user dashboard just like Instagram or Facebook has after their domain names. Example https://facebook.com/the.hardik.sisodia.page
Routes
Route::get('{username}', [DashboardController::class, 'index'])
->name('dashboard');
Route::get('contact', [ContactController::class, 'index'])->name('contact');
Controller
$user = User::where('username', $username)->get();
if (isset($user[0])) {
$data = [
'user' => $user ? $user : abort(404),
];
return view('frontend.user.dashboard', $data);
} else {
abort(404);
}
Issue
When I attempt to view the Contact page, I cannot view it because of a 404 error page not found. I don't understand where I am going wrong. I also tried to group dashboard route using {username} as a prefix in the route group but still faced the same issue.
As you're getting a parameter in your route address, you may have route conflicts.
Try adding a unique name before your route address, something like adding profile before the route
Route::get('profile/{username}', [DashboardController::class, 'index'])->name('dashboard');
Related
My laravel project has an API route by auth.basic middleware which is used id of the authenticated user in the controller. when I call it in postman it works well and I get 401 when the username or password is incorrect, but in laravel APITest which extends from DuskTestCase, authentication does not take place so I get 500 instead of 401 when the user's informations were incorrect. However, by correct information, I have the same error because auth() is null.
it is written like below, which is wrong?
api.php route:
Route::get('/xxxxxx/xxxxxx/xxxxxxx', 'xxxxx#xxxx')->middleware('auth.basic');
APITest:
$response = $this->withHeaders(['Authorization' => 'Basic '. base64_encode("{$username}:{$password}")])->get("/xxxxxx/xxxxxx/xxxxxxx");
You can use actingAs() method for authentication in tests.
An example from docs:
public function testApplication()
{
$user = factory(App\User::class)->create();
$this->actingAs($user)
->withSession(['foo' => 'bar'])
->visit('/')
->see('Hello, '.$user->name);
}
Another example that you can use for an API:
$user = User::factory()->create();
$response = $this->actingAs($user)->json('GET', $this->uri);
$response->assertOk();
For more information: https://laravel.com/docs/5.1/testing#sessions-and-authentication
The url right now is website.com/profile/1/edit but I wanted to just only show website.com/profile
I have tried using the route web.php
Route::resource('profile', 'ProfileController'); and Route::get('profile', 'ProfileController#edit');
You can create some route like that :
Route::get('my_profile','ProfileController#myProfile');
And the function like that :
public function myProfile (){
$user = Auth::User();
if($user){
/*
Your code and redirect
*/
}
else{
return back();
}
}
Don't forget this : use Auth; in above of file
If you want to edit profile of user who is logged in. Try getting the ID from session rather than sending it from the URL. Instead of this, you can also use post request to hide the id from URL.
I've made some bespoke pages in my admin of my site and they as the first segment of the URL.
e.g
/property-hartlepool
I thought of adding a trap all route into my routes file :
Route::get('{any?}', 'PagesController#view');
The problem I have is it's totally overwriting my other routes, I guess that's the name of a trap all route. However I'd like it to skip if it can't find a match.
I had a route for admin
/admin
But now it throws a 404 Error...
My PagesController#view method is :
public function view(Request $request)
{
$route = $request->segment(1); // $request->path();
// get page content
$page = Page::where('route', $route)->firstOrFail();
// If not full width, get four latest properties
//$properties = Property::latest_properties_for_frontend();
//metadata
$meta = get_metadata($page);
//page is Temporary
return view('frontend.'.themeOptions().'.page', [
'route' => $route,
'meta' => $meta,
'page' => $page
]);
}
Is their a better way of doing this, I do have other routes that sit at "top" level too. e.g...
Route::get('/property/{property}/{propertyId}', 'PropertiesController#property');
declare your trap Route as the last route.
Route::get('/admin', 'AdminController#view');
...
...
Route::get('{any?}', 'PagesController#view');
Basically i'm trying to send by email a link that lets you login with a specific account and then redirects you to a page.
I can seccessfully generate link and send them via email using URL functionalities in laravel using this code:
Generating the link:
$url = "some/page/".$travel_id;
$link = URL::temporarySignedRoute(
'autologin', now()->addDay(), [
'user_id' => 3,
'url_redirect' => $url,
]
);
And sending the mail:
Mail::send('emails.travel', $data, function ($message) use ($data) {
$message->from('mail#mail.com', 'blablabla');
$message->to('reciever#mail.com', 'blablabla')->subject('test');
});
There is a route that catches the link sent by mail that is supposed to log you in with the user (in this case, the one with the id '3') and redirect you to some page but when it redirects, it prompts you to the login page, as if you are not logged.
Here is the route:
Route::get('/autologin', function (Request $request) {
$user = User::findOrFail($request->user_id);
if (! $request->hasValidSignature()) {
abort(403);
}
Auth::login($user);
return redirect($request->input('url_redirect'));
})->name('autologin');
When i try to do a Auth::check() after the Auth::login($user); it returns true, so is the user logged in?
I also tried to use Auth::loginUsingId($request->user_id); with no different results.
Any idea of what's happening?
So i found the problem,
I was logging in with a backpack user but i was using the default auth feature of laravel.
Turns out i need to use: backpack_auth()->login($user); instead of Auth::login($user); if i want to login using a backpack user.
Also use backpack_auth()->check() instead of Auth::check().
I am new in laravel framework now I'm working fully developed website using Laravel. I have changed blog url form {id} to {id}/{name} like www.example.com/news/203 to www.example.com/news/203/title. It's working fine. but i need to redirect if old url enter into current url opened from cache or something else.
Route::get('{id}/{name}', 'EventController#show')
->name('events-detail')
->where([
"id" => "[0-9]+"
]);
You can define another route in which you will find the model by id and use its title to redirect the user to the new route:
Route::get('{id}', function ($id) {
$model = Model::findOrFail($id);
return redirect()->route('events-detail', ['id' => $id, 'name' => $model->name]);
});
Note that you have to change Model with the class you use for this route.
Create 2 routes and add below code.
Route::get('{id}/{name}', function () {
//new URL as you want
return redirect()->route({id}/{name});
});
Route::get('{id}', function () {
//as you want for simple URL
});
I'm assuming the name portion is not really used at all, except for SEO/friendlier urls. If this is the case, just make the name parameter optional, and there will be no need for a redirect:
Route::get('{id}/{name?}', 'EventController#show')
->name('events-detail')
->where([
"id" => "[0-9]+"
]);
This route will match /203 and /203/news-title.