laravel 4 , NotFoundHttpException - laravel-4

i have problem in submitting this form it produces NotFoundHttpException
{{Form::open(array('parsley-validate'=>'','url'=>url('dashboard/send_message')))}}
this is the function the form should execute in dashHome.php in dashboard folder
public function postSendMessage()
{
$validator = Validator::make(Input::all(), array(
'studentId' => 'required|exists:student,id',
'content' => 'required',
));
if ($validator->fails())
return Redirect::to('dashboard')->withErrors($validator);
$user = Auth::user();
$user->decrement('sms_credit');
return Redirect::to('dashboard')->with('messageSent',1);
}
this is the route in route.php
Route::group(array('prefix' => 'dashboard','before'=>'auth'), function() {
Route::get('/', 'dashHome#index');
});

You need to define a route for the url dashboard/send_message in your routes.php file, like this:
Route::group(array('prefix' => 'dashboard','before'=>'auth'), function()
{
Route::get('/', 'dashHome#index');
Route::post('send_message', 'dashHome#postSendMessage');
});

Related

How to fix Class not found error while making POST request?

In api.php I've described some routes. GET method works. Can't tell the same about POST method.
<?php
use Illuminate\Http\Request;
use App\UserUnfo;
Route::middleware('auth:api')->get('/user', function (Request $request)
{
return $request->user();
});
Route::get('/person', function() {
$person = [
'ip' => '127.0.0.1',
'name' => 'me'
];
return $person;
});
Route::post('/person', function(Request $request) {
$userInfo = UserInfo::create([
'name' => $request->input('name'),
'ip' => $request->input('ip')
]);
return $userInfo;
});
In web.php
Route::get('/home', 'HomeController#index')->name('home');
The error I've got
Class 'UserInfo' not found
You're using the wrong model it's spell mistake.
use App\UserUnfo;
To
use App\UserInfo;

Error on POST Request

I am developing in Laravel 5.3.
When reviewing the routes, I have the following:
Here it indicates that my routes are POST. But if in Postman I enter the URL (POST), I throw the following error
The strangest thing is that on my local server it works fine, this error occurred to me on the production server.
My code is:
api.php
Route::group(['middleware' => ['api', 'auth:api']], function() {
require_once 'Routes/Api/ProductRoute.php';
});
routes/Routes/Api/UserRoute.php
<?php
Route::post('user/authenticate', [
'as' => 'api.user.authenticate',
'uses' => 'Api\UserController#authenticate'
]);
Route::post('user/register', [
'as' => 'api.user.register',
'uses' => 'Api\UserController#register'
]);
/app/Http/Controllers/Api/UserController.php
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
$user = Auth::user();
\DB::table('oauth_access_tokens')
->where('user_id', $user->id)
->where('name', $request->platform)
->update(['revoked' => 1]);
$token = $user->createToken($request->platform)->accessToken;
return Controller::apiResponse(1, compact('user', 'token'));
} else{
return Controller::apiResponse(-1);
}
}

Laravel 5.3 : Pages not loading properly

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

Profile page for logged user - laravel 5.2

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');

session is not persisting.killed after redirect

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').

Resources