notfoundhttp exception while post request - php

I have researched all the previous answers of this similar question, yet I couldnt found any.
I am simply accessing the function which I have made custom
Routes
Route::post('dashboard', 'Admin\UserController#index');
UserController.php
<?php
namespace App\Http\Controllers;
use DB;
use Session;
use App\Http\Requests;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request)
{
echo "welcome"
}
}
When I am trying this,it throws me
NotFoundHttpException in RouteCollection.php line 161:
error.
Update
My all routes are
Route::get('/', function () {
return view('welcome');
});
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration routes...
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
// Dashboard routes
Route::post('dashboard', 'Admin\UserController#index');
Route::controllers([
'password' => 'Auth\PasswordController',
]);

As You mentioned the issue its Not Getting the Path of Controller in namespace:
Try To Change namespace with this
namespace App\Http\Controllers\Admin;
and your Routes will be like this: Route::post('dashboard', 'UserController#index');

In the UserController you have used
namespace App\Http\Controllers;
which is not correct with the route you are using. It should be namespace
App\Http\Controllers\Admin;
Secondly, it will be proper if you have the route of dashboard as get:
Route::get('dashboard', 'Admin\UserController#index');
Routes
Route::post('dashboard', 'Admin\UserController#index');
UserController.php
<?php
namespace App\Http\Controllers\Admin;
use DB;
use Session;
use App\Http\Requests;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request)
{
echo "welcome"
}
}

namespace you need to add Admin in your controller
namespace App\Http\Controllers\Admin;
Since in your route you have Admin
Route::post('dashboard', 'Admin\UserController#index');
Updated
As per your comment you are accessing via
http://localhost/travelling/dashboard but if you are using localhost then you have to add public
http://localhost/travelling/public/dashboard
Also make sure since its POST reuqest you cant access Url directly from the browser.you need to use curl or html form with csrf token
Update
Since you have added $this->middleware('auth'); in constructor so you have to login

Related

Laravel reset password controller and function

I am new to laravel and working on one laravel project. I am getting issue in finding larvel reset password controller and function.
Here is the html form code where the route is mentioned
<form method="POST" action="{{ route('password.update') }}">
Here is the route path
Route::get('/password/reset/{token}/{email}', 'Auth\ResetPasswordController#showResetForm')->name('password.reset');
I try to check the ResetPassword Controller but there is no showResetForm in this file. Can anybody tell me where I can check this function?
Here is the code of ResetPassword Controller
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest');
}
}
Any help is appreciated
Thanks and Regards
you can check showResetForm function in
Illuminate\Foundation\Auth\ResetsPasswords
https://github.com/guiwoda/laravel-framework/blob/master/src/Illuminate/Foundation/Auth/ResetsPasswords.php
ResetPasswordController used ResetsPasswords trait.
In this trait you can find showResetForm() method.
You can overwrite this method in your ResetPasswordController controller.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest');
}
/**
* Display the password reset view for the given token.
*
* If no token is present, display the link request form.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showResetForm(Request $request)
{
$token = $request->route()->parameter('token');
return view('auth.passwords.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
}

Why an api request is going to web.php route from api.php route?

I am working with laravel RestfulApi project. I am facing an unexpected trouble. When I try to send an API request to api.php route, it goes to the web.php route. But if I don't use validation in my controller file, the code runs well. I only get the above problem when using validation. Below is my code.
Api.php Routes
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::get('/showingData','UserContactController#show');
Route::post('/storingData','UserContactController#store');
Web.php Routes
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
UserContactController.php
<?php
namespace App\Http\Controllers;
use App\UserContact;
use Illuminate\Http\Request;
class UserContactController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'name'=>'required|max:5'
]);
$userContact=new UserContact();
$userContact->name=$request->input('name');
$userContact->email=$request->input('email');
$userContact->description=$request->input('description');
$userContact->visibility=$request->input('visibility');
$userContact->created_by=$request->input('created_by');
$userContact->save();
return response()->json([
"message"=>"student record created"
],201);
}
}
UserContac.php Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserContact extends Model
{
//
}
----------
My Postman url
http://amaderproject.test/api/storingData

Use custom middleware in controller

I created middleware: php artisan make:middleware CheckUserStatus
In this middleware I have:
namespace App\Http\Middleware;
use Closure;
class CheckUserStatus
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if(Auth()->check() AND Auth()->user()->status === 0) { // user is logged in but it is blocked
auth()->logout();
return redirect('/');
}
return $next($request);
}
}
Then, one of my controller I have:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Middleware\CheckUserStatus;
class productsController extends Controller
{
public function __construct () {
$this->middleware('auth');
$this->middleware('CheckUserStatus');
}
}
This gives ReflectionException - Class CheckUserStatus does not exist
What I'm doing wrong ?
You need to register your middleware if you want to reference it by a string key. Check out the docs here.
Alternatively, you could use the fully qualified class name: try CheckUserStatus::class instead of 'CheckUserStatus'.
You need to use the fully qualified class name:
Either:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class productsController extends Controller
{
public function __construct () {
$this->middleware('auth');
$this->middleware('\App\Http\Middleware\CheckUserStatus');
}
}
or
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Middleware\CheckUserStatus;
class productsController extends Controller
{
public function __construct () {
$this->middleware('auth');
$this->middleware(CheckUserStatus::class); //No quotes
}
}
You need to add your middleware in kernel.php
protected $routeMiddleware = [
'your_desire_name'=>\App\Http\Middleware\CheckUserStatus::class,
];

Add middleware to controller in __construct in Laravel

I am trying to assign a middleware in __construct of a controller based on Laravel docs but it throws the follwing error:
BadMethodCallException
Method App\Http\Controllers\MyController::middlware does not exist.
that is my controller class:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class MyController extends Controller
{
public function __construct()
{
$this->middleware('myauth');
}
/** something */
public function index()
{
return view('test.hi', ['name' => 'Moh']);
}
}
And here is the middleware code:
<?php
namespace App\Http\Middleware;
use Closure;
class myauth
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
echo time().'<br>';
return $next($request);
}
}
Laravel version: 6.5.2
Where am I doing wrong?
Middleware can be specified within controller's constructor
public function __construct() {
$this->middleware('auth');
}
For whole controller:
$this->middleware('auth');
Only for a particular action:
$this->middleware('auth')->only('index');
For whole controller except particular action:
$this->middleware('auth')->except('store');
The function is middleware, you have a typo, missing an e.
Firstly ask to you, Your error is middlware name is incoorect you missed e after that check the below middleware process.
Laravel Middleware - Middleware acts as a middleman between a request and a response.
Firstly goto project folder and open cmd and use this command
php artisan make:middleware MiddlewareName
after that go to App\Http\kernel.php and add one lines on $routeMiddleware
'user_block' => \App\Http\Middleware\MiddlewareName::class
After that goto your middleware
In handle function (write your own middleware code)
In routes use your middleware -
Route::group(['middleware' => ['user_block']],
function () {
Route::get('/logout', array('uses' => 'Auth\LoginController#logout'));
});
If you used this middleware in specific controller in __construct in any controller just write a line
namespace App\Http\Controllers;
use App\User;
class UserController extends Controller {
public function __construct() {
$this->middleware('user_block');
}
}
If you want this middleware for just one action in the controller you can add this middleware to the route :
Route::get('/login', 'LoginController#login')->middleware('user_block');
If you used this middleware in specific controller in specific 1-2 function just write this line in __construct functiono in controller
public function __construct()
{
$this->middleware('user_block')->only(['login','register']);
}

php Laravel ~ Attribute [controller] does not exist

I am trying to set up an Route Controller in my Laravel project and I have set up the controller and also the route.
However, when I load the route in the web.php then it produces an error when I try to navigate to that page in the browser of Attribute [controller] does not exist
Here is the code..
<?php
namespace CMS\Http\Controllers\Auth;
use CMS\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers {
logout as performLogout;
}
/**
* Where to redirect users after login.
*
*/
protected $redirectTo;
/**
* Create a new controller instance.
*
*/
public function __construct()
{
$this->redirectTo = route('backend.dashboard');
$this->middleware('guest')->except('logout');
}
public function logout(Request $request)
{
$this->performLogout($request);
return redirect()->route('auth.login');
}
}
And then in the web.php I have this...
Route::controller('auth', 'Auth\LoginController', [
'getLogin' => 'auth.login'
]);
The controller method is deprecated since Laravel 5.3. But now, you can use the resource method, which is meant for the same purpose as the controller method.
Like This:
Route::resource('auth', 'LoginController');
or
Route::get('/auth','LoginController');
Route::post('/auth','LoginController');

Categories