Here is the code.If sign in it goes to /dashboard route. but after I go to other route user session is not persisting(by dd I found this).thanks in advance if you solve, I spent hours on this.
Route::group(['middleware' => 'web'],function(){
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::get('/dashboard' , [
'uses' => 'UserController#GetDashboard',
'as' => 'dashboard'
]);
Route::post('/signin' , [
'uses' => 'UserController#postSignin',
'as' => 'signin'
]);
});
in my login controller
public function postSignin(Request $request)
{
if(Auth::attempt(['email' => $request['email'],'password' => $request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
$request is an object, not an array. Try using $request->get('email').
Related
I have question about Laravel.
I want display SEO tag automatically from Database but I do not know how to do.
I have route like this
Route::get('/', [
'uses' => 'SeoController#index',
'as' => 'homepage'
]);
Route::get('/about', [
'uses' => 'SeoController#index',
'as' => 'about'
]);
From SeoController I want to display view base on Route url;
public function index()
{
switch ($route) {
case '/':
$title = "Homepage";
return view('welcome', ['title'=> $title]);
break;
case '/about':
$title = "About page";
return view('about', ['title'=> $title]);
break;
default:
break;
}
}
How can I check $route to know which route come?
Thank you so much
I would love to suggest a better way of doing this in Laravel.
In Laravel, you would want to define different controller methods for each pages and return a view like so:
class SeoController extends Controller
{
public function home()
{
return view('home');
}
public function about()
{
return view('about');
}
public function contact()
{
return view('contact');
}
}
Ensure you have the routes registered in web.php as:
Route::get('/', [
'uses' => 'SeoController#home',
'as' => 'homepage'
]);
Route::get('/about', [
'uses' => 'SeoController#about',
'as' => 'about'
]);
Route::get('/contact', [
'uses' => 'SeoController#contact',
'as' => 'contact'
]);
And also ensure you have the corresponding blade file for each of these views in the view folder.
auth()->attempt($credentials, true) is working but its redirecting to /login however! I didn't do anything like that in middleware for example. Can anyone knows what is going on? Thanks
LoginController
public function login(Request $request)
{
$this->validateLogin($request);
$credentials = $request->only('email', 'password');
if (auth()->attempt($credentials, true)) {
// return 'it works';
return redirect()->to('/');
}
return redirect()->back()->withErrors([
'msg' => trans('labels.others.credentials_not_valid')
]);
}
Routes
Route::group(['middleware' => 'auth'], function () {
Route::get('/', ['as' => 'home', 'uses' => 'HomeController#index']);
});
When the user log in or register, he is supposed to go the dashboard, but instead of it, it is being located to the /login page, which is not even there and hence error occours:
Sorry, the page you are looking for could not be found.
1/1
NotFoundHttpException in RouteCollection.php line 161:
Routes:
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::group(['middleware' => ['web']], function(){
Route::get('upload',function(){
return view('files.upload');
});
Route::get('/wallet',[
'uses' => 'WalletController#getwallet',
'as' => 'wallet'
]);
Route::post('/addmoney',[
'uses' => 'WalletController#addmoney',
'as' => 'addmoney'
]);
Route::post('/signup',[
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
Route::post('/signin',[
'uses' => 'UserController#postSignIn',
'as' => 'signin'
]);
Route::get('/dashboard',[
'uses' => 'UserController#getDashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);
Route::post('/handleUpload','FilesController#handleUpload');
Route::get('/pay', ['as' => 'pay', 'uses' => 'PaymentController#pay']);
# You will need one more.
Route::get('/payment/status', ['as' => 'payment_status', 'uses' => 'PaymentController#status']);
/**
* Using Named Routs to demonstrate all the possibilities.
*/
});
User controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
use InvalidConfirmationCodeException;
use Flash;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class UserController extends Controller
{
public function getDashboard(){
return view('files.dashboard');
}
public function postSignUp(Request $request)
{
$this -> validate($request,[
'email' => 'required|email|unique:users',
'name' => 'required|max:20',
'password' => 'required|min:4'
]);
$email = $request['email'];
$name = $request['name'];
$password = bcrypt($request['password']);
$user = new User();
$user->email =$email;
$user->name = $name;
$user->password = $password;
$user->save();
return redirect()->route('dashboard');
//Auth::login($user);
}
public function postSignIn(Request $request)
{
$this -> validate($request,[
'email' => 'required',
'password' => 'required'
]);
if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
}
WalletController
public function getwallet(){
return view('files.wallet');
}
public function addmoney(Request $request){
$this->validate($request,[
'amount'=>'required'
]);
$amount = $request['amount'];
$wallet = new Wallet();
$wallet->amount=$amount;
$wallet->save();
return redirect()->route('/addmoney');
}
Even when I try localhost:8000/dashboard , it loads as localhost:8000/login as shows same error.Also, same problem occurs when I try to load /addmoney page, when the user submit amount and redirect to the next addmoney page.
I see your dashboard view view is in file.controller but you use the auth middleware. Put your dashboard view in your dashboard view in the auth folder.
Default in Laravel: resources/views/auth/yourviewhere
The problem is, when i submit the form, the browser throw MethodNotAllowedHttpException in RouteCollection.php line 219:
My route
Route::group(['middleware' => ['web']], function () {
Route::resource('dash/reports', 'Dash\\ReportsController');
});
/* ruote for Admin */
Route::group(['middleware' => ['role:admin']], function () {
Route::resource('dash/categories', 'Dash\\CategoriesController');
});
Route::group(['middleware' => ['role:admin']], function () {
Route::resource('dash/roles', 'Dash\\RolesController');
});
Route::group(['middleware' => ['role:admin']], function () {
Route::resource('dash/permissions', 'Dash\\PermissionsController');
});
Route::group(['middleware' => ['role:admin']], function () {
Route::resource('dash/users', 'Dash\\UsersController');
});
/* another routes */
Route::auth();
Route::get('/profile-edit/{id}', 'Dash\\UsersController#editUser');
My controller:
public function editUser($id)
{
$auth = Auth::user()->id;
$user = User::findOrFail($id);
if($auth == $user->id){
return view('dash.users.update_profile', compact('user'));
}
return redirect('errors/404');
}
public function storeUpdatedUser($id, Request $request)
{
$this->validate($request, ['email' => 'required', 'name' => 'required', 'password' => 'required', 'surname' => 'required', ]);
$user = User::findOrFail($id);
$user->update($request->all());
$user->password = bcrypt($request->password);
$user->save();
Session::flash('flash_message', 'User updated!');
return redirect('/');
}
the view:
{!! Form::model($user, [
'method' => 'PATCH',
'url' => ['/profile-edit', $user->id],
'class' => 'form-horizontal'
]) !!}
........
WHere is the problem? And another problem is the field "password" show me a hased password, anyone can explaine me ?
You are sending a PATCH request to a get route:
Route::get('/profile-edit/{id}', 'Dash\\UsersController#editUser');
{!! Form::model($user, [
'method' => 'PATCH',
'url' => ['/profile-edit', $user->id],
'class' => 'form-horizontal'
]) !!}
Change the route from get to patch
Route::patch('/profile-edit/{id}', 'Dash\\UsersController#editUser');
Try to use Route::put('/profile-edit/{id}'...
solved:
Route::get('/profile-edit/{id}/edit', 'Dash\\UsersController#editUser');
Route::patch('/profile-edit/{id}', 'Dash\\UsersController#storeUpdatedUser');
I have a controller UserController.php which contains the following methods:
getIndex(), getAll(), getCreate(), postStore(), getShow(), getEdit(), putUpdate(), getDestroy(), getLogin(), getDashboard(), and getLogout().
I have included the following codes into the routes.php
Route::get('/', function()
{
return View::make('hello');
});
Route::get('users/all/', [ 'as' => 'users.index', 'uses' => 'UserController#getAll']);
Route::get('users/create/', [ 'as' => 'users.getCreate', 'uses' => 'UserController#getCreate']);
Route::get('users/{all}/edit', 'UserController#getEdit');
Route::put('users/update/{id}', [ 'as' => 'users.putUpdate', 'uses' => 'UserController#putUpdate']);
Route::controller('users', 'UserController');
I can access the pages like
http://localhost/testlaravell/users/
or
http://localhost/testlaravell/users/add
etc.
Now, I want that only logged in users can access the pages, other wise s/he will be redirect to the login page http://localhost/testlaravell/login
The methods for login under UserController.php as follows:
public function postSignin() {
$rules = array(
'username' => 'required', // make sure the username is an actual username
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);
// if the validator fails, redirect back to the form
if ($validator->fails()) {
return Redirect::to('users/login')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
} else {
// create our user data for the authentication
$userdata = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
// attempt to do the login
if (Auth::attempt($userdata)) {
return Redirect::to('users/dashboard')->with('message', 'Welcome User');
} else {
// validation not successful, send back to form
return Redirect::to('users/login')->with('message', 'Sorry!! Username/ Password seems wrong.');
}
}
}
public function getLogin() {
return View::make('users.login');
}
You'll want to make use of the Auth Filter in Laravel 4
You can wrap all your routes in a group and specify that filter, in your case the code would be something like this.
Route::group(array('before' => 'auth'), function()
{
Route::get('users/all/', [ 'as' => 'users.index', 'uses' => 'UserController#getAll']);
Route::get('users/create/', [ 'as' => 'users.getCreate', 'uses' => 'UserController#getCreate']);
Route::get('users/{all}/edit', 'UserController#getEdit');
Route::put('users/update/{id}', [ 'as' => 'users.putUpdate', 'uses' => 'UserController#putUpdate']);
Route::controller('users', 'UserController');
});
You can checkout the documentation on Route Groups here