I have a PHP Laravel app that has an Elequent Model for EmailTokenUrlLogin
and a Controller to login a user by accessing a URL with a token value in the URL.
I would like to have a function that will create a new Model/DB record to generate the token for a user and be able to call this function from any other Controller or Model in my app.
Right now I have it in my Login Controller however I am not happy with this as it requires either a POST request to post all the required function parameters or else a GET request with a long complicated URL of parameters.
So I want my function below generateShortLifeEmailTokenUrl($user_id, $email, $expireType, $expireNumber = '')
to live somewhere in the app in which I can call it from anywhere.
Where would a function like below best be located and how to make it callable from other models and controllers?
public function generateShortLifeEmailTokenUrl($user_id, $email, $expireType, $expireNumber = '')
{
if(Auth::user()->isAdmin){
// generate a new token
// ... other logic here......
// create model and DB record and return the created token value
$emailTokenUrlLoginData = new EmailTokenUrlLogin;
$emailTokenUrlLoginData->user_id = $user_id;
$emailTokenUrlLoginData->email = $email;
$emailTokenUrlLoginData->token = $createToken();
$emailTokenUrlLoginData->expireType = $expireType;
$emailTokenUrlLoginData->expireNumber = $expireNumber;
$emailTokenUrlLoginData->save();
// return generated token
return $emailTokenUrlLoginData->token;
}
}
If you're working with this function a lot, you can create global helper:
Put it in
helpers.php
and add this tocomposer.json
to load the helpers: