So I have these routes defined in my routes.php:
Route::post('/register', 'Auth\AuthController#Register');
Route::post('/login', 'Auth\AuthController#Login');
Route::get('/verify/{$key}', 'Auth\AuthController#Verify');
The first two works fine. But for some reason the third one [ /verify/{$key} ] throws a NotFoundHttpException.
The verify route calls the Verify() function in my AuthController as shown below.
public function Verify($key)
{
$user = User::Where('verification_code', $key);
if(!$user)
{
flash()->error('Error Occurred in verification.');
return redirect('/login');
}
$user->verified = 1;
$user->verification_code = null;
$user->save;
flash()->success('Account Successfully Verified.');
return redirect('/login');
}
When calling the php artisan route:list from the terminal the verify/{key} shows up.
Any help would be much appreciated.
Change this:
Route::get('/verify/{$key}', 'Auth\AuthController#Verify');
to
Route::get('/verify/{key}', 'Auth\AuthController#Verify');
You don't have to add $ along with the variable in the route.
Related
I have implemented a custom middleware in my application. And I have found an issue.
It somehow does't reach the abort(403), but instead gives me this error:
"No query results for model [App\Models\Meeting]"
My goal is to redirect the user back if they're on a meeting page that doesn't exist/exists to another user. But instead it doesn't redirect me, but sends me that error.
Here is my middleware:
public function handle(Request $request, Closure $next)
{
$userid = Auth::user()->id;
$meetings = Meeting::where('user_id', $userid)->get();
$url = $request->segment(4);
$idArr = [];
foreach ($meetings as $meeting) {
array_push($idArr, $meeting->id);
}
if (!in_array($url, $idArr)) {
abort(403);
} else {
return $next($request);
}
}
The url is just the id of the meeting, so that works fine.
For your info, it can reach the middleware, so it is connected/added properly. It just doesn't access the if code.
For what it's worth, here is my controller where I connect the middleware.
public function __construct()
{
$this->middleware(CheckMeeting::class)->only('show');
}
Can anyone explain or help me? Thanks in advance.
edit:
These are my routes
Route::apiResource('login', LoginController::class)->only(['index', 'store', 'show']);
Route::delete('logout', [LoginController::class, 'destroy']);
Route::apiResource('register', RegisterController::class)->only('store');
Route::apiResource('meeting', MeetingController::class)->middleware('auth:sanctum');
Route::post('keywords', [FileController::class, 'index']);
Route::apiResource('file', FileController::class)->middleware('auth:sanctum')->except(['index']);
Route::apiResource('userignoreword', IgnoreWordController::class)->only(['store'])->middleware('auth:sanctum');
the URL I use is: http://xxx/meeting/10
xxx is the placeholder as it is not relevant.
Try using response helper
return response(null,403)
Everything was working fine before I installed a package https://github.com/kreait/firebase-php.
Now I get 404 error on adding new Routes.
Old Routes are working fine, but new routes are not working
Web.php
//old routes
Route::get('/', function () {
return view('welcome'); //working
});
Route::get('email', 'EmailController#sendEmail'); /working
Route::get('/counter',function() {
$counter = Redis::incr('counter'); //working
return $counter;
});
//new routes
//Firebase admin SDK
Route::get("fire","FirebaseSdkController#fire");
Route::get("say-hello", function(){
return "hello";
});
FirebaseSdkController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Kreait\Firebase;
use Kreait\Firebase\Messaging\CloudMessage;
class FirebaseSdkController extends Controller
{
public function fire()
{
$firebase = (new Firebase\Factory())->create();
$messaging = $firebase->getMessaging();
$topic = 'News';
$message = CloudMessage::withTarget('topic', $topic)
->withNotification("Hello") // optional
// ->withData($data) // optional
;
$message = CloudMessage::fromArray([
'topic' => $topic,
'notification' => [/* Notification data as array */], // optional
'data' => [/* data array */], // optional
]);
$messaging->send($message);
}
}
This is what I have tried so far -
php artisan route:list
shows the new routes in the list
php artisan route:clear
php artisan config:clear
composer dump-autoload
None of them solved the issue.
Update
Now, if I delete any route and do php artisan route:clear, I still can access the route. I don't know what's happening please help.
In my ChallengesController I have these routes:
public function show($id) {
$challenge = Challenge::find($id);
if (!$challenge) {
return back()->with('error', 'Challenge does not exist');
}
$projects = $challenge->projects;
return view('challenges.show')->with(['challenge' => $challenge, 'projects' => $projects]);
}
public function create() {
if (auth()->user()->role === 'user') {
return back()->with('error', 'You are unauthorized to do that');
}
return view('challenges.create');
}
In my web.php routes I have these routes:
Route::get('/challenges/{id}', 'ChallengesController#show');
Route::get('/challenges/create', 'ChallengesController#create');
Whenever I want to go to /challenges/create it thinks I have to go to /challenges/{id} and is thinking the {id} is "create". But in my other controller where I just specified
Route::resource('projects', 'ProjectsController');
it has the same route structure when I do php artisan route:list, but it's working and my custom /challenge routes are not.
Is there a way to override the /challenges/create or am I doing something wrong. I am using Laravel version 5.7.20.
or even more simpler, change the order of declaration:
Route::get('/challenges/create', 'ChallengesController#create');
Route::get('/challenges/{id}', 'ChallengesController#show');
From Laravel documentation
You may constrain the format of your route parameters using the where
method on a route instance. The where method accepts the name of the
parameter and a regular expression defining how the parameter should
be constrained:
Route::get('challenges/{id}', function ($id) {
//
})->where('id', '[0-9]+');
Now only numeric values will be accepted as the parameter id.
In laravel I've simply done this:
Route::group(["middleware" => "admin"], function() {
Route::get("/", "UserController#index")->name("user_index");
});
Route::group(["middleware" => "user", "as" => "User::"], function() {
Route::get("/", "DocumentController#index")->name("user_index");
});
The problem is when I am logged in as my Admin auth middleware, when going to "/" my browser returns too many redirects and stops. I'm guessing because the second route is removing this as when I print out php artisan route:list there is only one result for "/" and that's with the user middle's parameters so it's defo overriding the previous route.
What I don't understand is why would it do this is both have a separate middleware?
Both middlewares are extremely simple. Below is my admin
public function handle($request, Closure $next)
{
if ( Auth::check() && Auth::user()->hasRole("customer_service") )
{
return $next($request);
}
return redirect("/");
}
And my user's middleware is exactly alike except the role is different
This is probably wrong but this is what I did to fix this particular issue with the above.
public function index() {
return \Auth::user()->hasRole("trainer") ? \App::call("App\Http\Controllers\Trainer\UserController#index")
: \App::call("App\Http\Controllers\User\UserController#index");
}
I create middleware for an admin role using the following code:
php artisan make:middleware AdminMiddleware
After that, I create a route for the login page:
Route::get('admin/login', ['middleware'=>'web','as'=>'admin.login','uses'=>'AdminController#loginView']);
Route::post('admin/login',['middleware'=>'web','as'=>'admin.login','uses'=>'AdminController#login']);
Route::group(['prefix'=>'admin','middleware' => ['auth.admin','web']], function()
{
Route::get('/', ['as'=>'admin.home','uses'=>'AdminController#index']);
Route::get('/home', ['as'=>'admin.home','uses'=>'AdminController#index']);
});
And the controller is
class AdminController extends Controller
{
//
function index(){
return 'welcome';
}
function loginView(){
return view('admin.login');
}
function login(Request $request){
$error = $this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:5',
]);
$email = $request->input('email');
$password = $request->input('password');
$remember = $request->input('remember');
if (Auth::attempt(['email' => $email, 'password' => $password,'type'=>'admin'], $remember)) {
// Authentication passed...
Auth::login(Auth::user(), $remember);
return redirect()->route('admin.home');
}
else{//('message', 'Login Failed')
return redirect()->route('admin.login')->withErrors($request->all(), "message")->withInput();
}
}
}
And in AdminMiddleware
public function handle($request, Closure $next)
{
var_dump(Auth::user());
if(!Auth::check()){
return redirect()->route('admin.login')->withErrors('You are not logged in');
}
elseif ($request->user()->type != 'admin'){
dd($request->user());
return redirect()->route('admin.login')->withErrors('You have not authority');
}
return $next($request);
}
The error is: I always get null for each $request->user() or Auth:user in AdminMiddleware.
You're passing the middleware to the route group in an incorrect order.
Right now you have this order ['auth.admin', 'web'] which means that the auth.admin middleware will be executed before the middleware from the web group, and since web contains the StartSession middleware, you won't have any session in auth.admin which is needed to get the authenticated user.
So simply switch the middleware order like so:
Route::group(['prefix'=>'admin','middleware' => ['web', 'auth.admin']], function () {
// now the session is set up in `web` and then you have it in `auth.admin`
});
In my case the actual problem was a blank line before the PHP starting tag.
I used following core PHP function to redirect instead of returning a view file from controller or instead of using Laravel redirect.
header('Location: /');
It printed the actual file which had a blank line. Removing this line fixed my problem.
There were thousands of files in my code base. My assumption was that I had tried different scripts to find such blank lines at start of any file and there was no such file as per those scripts results. I assumed there was no blank line in any of my files. But header('Location: /') proved that my assumption was not wrong, and I was working on the wrong lines.