how to call email controller from another controller by passing some data - email

i want to send an email when a candidate applies for a jobpost on my site to the candidate.
my email controller
public function sendEmail(candidate $candidate, jobPost $jobPost){
$company= $jobPost->Company;
$name= $candidate->name;
$email= $candidate->email;
$job= $jobPost->title;
$data= [
'title'=>$company,
'content'=>'this is sent by hrlead',
];
Mail::send('email.test',$data,function ($message){
$message->to('saberi1365#gmail.com', 'vahid')->subject('hello vahid');
});
return back();
}
my route:
Route::get('/email/{candidate}/{jobpost}', 'EmailsController#sendEmail');
i have an apply method which i want it to call the rout for it to send the email. but since i dont know how to return a dynamic url i am lost.
so far at the end of my apply method i have put:
return `redirect`('/email/' + $candidate +'/'+$jobpost);
which wont work
now i dont know how to call the

One simple way is that you can extend your Email Controller from the other controller, then you can access all the functions of your Email controller.
OtherController : EmailController
Also
OtherController extends EmailController

Related

Getting method name related to a rest service

I wanted to know if there exist a way of retrieving the actual method name associated to a rest service provided. Lets suppose my url is http://localhost:8080/v1/mytesturl now i want to retrieve the actual method name that is associated with this url.
Actually we are maintaining some key/value pair specific to the method that we have created and i need to make some checks based on the method name that gets executed using these values.
Plz let me know if there exist some way to do that..
Simply get the method name from the Object class.
#RestController
#RequestMapping("")
public class HomeController {
#RequestMapping("/mytesturl")
#ResponseBody
public String getMethodName() {
return new Object(){}.getClass().getEnclosingMethod().getName();
}
}
i got the solution by using this
Map<RequestMappingInfo, HandlerMethod> handlerMethods = RequestMappingHandlerMapping.getHandlerMethods();
HandlerExecutionChain handler = RequestMappingHandlerMapping.getHandler(requestr);
HandlerMethod handler1 = null;
if(Objects.nonNull(handler)){
handler1 = (HandlerMethod) handler.getHandler();
handler1.getMethod().getName()
}
this provide me with what i wanted..

Using a Mailable instead of a MailMessage for password reset emails in Laravel 5.5

I'd like my application to use Mailables for all of the emails it sends, so I created my own ResetPasswordEmail class that extends Mailable. Then I created my own ResetPassword notification class that extends the vendor class of the same name and overrode the toMail method as follows:
public function toMail($notifiable)
{
return (new ResetPasswordEmail())->with('token', $this->token);
}
Then I overrode the sendPasswordResetNotification from the CanResetPassword trait in my User model like this:
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPassword($token));
}
Calling my custom ResetPassword notification class.
The problem is that if I use the default method of creating a MailMessage and sending it, it automatically populates the 'to' field with the user's email. But when I use my ResetPasswordEmail mailable class, it doesn't.
Is there a good way to get it to work like that with my custom mailable?
Well in the end I just set the "to" field for my Mailable instance like this:
public function toMail($notifiable)
{
return (new ResetPasswordEmail())->with('token', $this->token)->to($notifiable->email);
}
Since $notifiable is an instance of the User model in this case, I can get the email like that. I don't know if this is the best way to do it, but it works.

MVC 5 - Redirect to a controller from anywhere

I am developing a MVC 5 internet application, and I wish to redirect to a controller/action result from anywhere in my application.
The controller name is "Manager", and the action result is "TestAction"
In an action result, the following code can be used:
RedirectToAction("TestAction", "Manager")
I have also coded a filter attribute as follows:
filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary{{ "controller", "Manage" }, { "action", "TestAction" } });
Is there a generic way to send the user to a specific controller, action result from anywhere in a MVC application? I specifically wish to do this in an extension method.
Thanks in advance.
You can do it by simply Response.Redirect (taking the domain path and append the url).
Did you try the answer like this one https://stackoverflow.com/a/18126733/713789
In your controller you can use RedirectToAction("TestAction", "Manager") to redirect and in your view you can write windows.location.href("Manager","TestAction").
What i've done in such cases:
Dictionary<string, object> dictionary = new Dictionary<string, object>();
//
//...add parameters...
dictionary.Add("controller", "ControllerName");//ex: Home for HomeController
dictionary.Add("action", "MyMethod");
return RedirectToRoute("Default", dictionary);//"Default" is the name of the rout you're using

How can I call a RIA service from another RIA service?

In my authentication service, I would like to call methods (query or invoke) on my User service to validate credentials. So, for example:
protected override AuthUser ValidateCredentials(string name, string password,
string customData, out string userData)
{
AuthUser user = null;
userData = null;
using (UserService svc = new UserService())
{
if (the result of a call on UserService shows a valid username/password)
{
//Create the user object
user = new AuthUser()
{
Name = name,
UserId = // that user's UserId
};
}
if (user != null)
{
//Set custom data fields for HTTP session
userData = user.UserId.ToString();
}
}
return user;
}
The results I'm finding when searching for things like "call ria service from another ria service" and similar are unrelated to actually calling one from another. Am I doing something wrong from a paradigm point of view? If not, how the heck do you do this? :)
Aggregating DomainServices when all you want to do is Query is pretty easy. Something like
new MyDomainService().GetUser(userName)
should work just fine. However, when you're trying to Submit or Invoke it become trickier because you'll need to initialize and dispose the DomainService. It's been a while since I did this, but I think you can override Initialize and Dispose in your parent DS to call through to the methods in your child DS. For submitting, you won't be able to call the methods directly. Instead you'll need to create a ChangeSet and call the DS.Submit method.
Also, for your scenario, it might be worth checking out the custom authentication sample here. It's a slightly different approach for what you're trying to do.

ZEND Controllers -- How to call an action from a different controller

I want to display a page that has 2 forms. The top form is unique to this page, but the bottom form can already be rendered from a different controller. I'm using the following code to call the action of the other form but keep getting this error:
"Message: id is not specified"
#0 .../library/Zend/Controller/Router/Rewrite.php(441): Zend_Controller_Router_Route->assemble(Array, true, true)
My code:
First controller:
abc_Controller
public function someAction()
{
$this->_helper->actionStack('other','xyz');
}
Second controller:
xyz_Controller
public function otherAction()
{
// code
}
Desired results:
When calling /abc/some, i want to render the "some" content along with the xyz/other content. I think I followed the doc correctly (http://framework.zend.com/manual/en/zend.controller.actionhelpers.html) but can't find any help on why that error occurs. When I trace the code (using XDebug), the xyz/other action completes ok but when the abc/some action reaches the end, the error is thrown somewhere during the dispatch or the routing.
Any help is greatly appreciated.
You can accomplish this in your phtml for your someAction. So in some.phtml put <?php echo $this->action('other','xyz');?> this will render the form found in the otherAction of XyzController
The urge to do something like this is an indication you're going about it in totally the wrong way. If you have the urge to re-use content, it should likely belong in the model. If it is truly controller code it should be encapsulated by an action controller plugin
In phtml file u can use the $this->action() ; to render the page and that response would be added to current response ..
The syntax for action is as follows::
public function action($action, $controller, $module = null, array $params = array())
You can create new object with second controller and call its method (but it`s not the best way).
You can extend your first controller with the second one and call $this->methodFromSecond(); - it will render second form too with its template.
BTW - what type of code you want to execute in both controllers ?
Just an update. The error had absolutely nothing to do with how the action was being called from the second controller. It turns out that in the layout of the second controller, there was a separate phtml call that was throwing the error (layout/abc.phtml):
<?php echo $this->render('userNavigation.phtml') ?>
line of error:
echo $this->navigation()->menu()->renderMenu(...)
I'll be debugging this separately as not to muddy this thread.
Thanks to Akeem and hsz for the prompt response. I learned from your responses.
To summarize, there were 3 different ways to call an action from an external controller:
Instantiate the second controller from the first controller and call the action.
Use $this->_helper->actionStack
In the phtml of the first controller, action('other','xyz');?> (as Akeem pointed out above)
Hope this helps other Zend noobs out there.
Hm I can't find and idea why you need to use diffrent Controlers for one view. Better practise is to have all in one Controller. I using this like in this example
DemoController extends My_Controller_Action() {
....
public function indexAction() {
$this->view->oForm = new Form_Registration();
}
}
My_Controller_Action extends Zend_Controller_Action() {
public function init() {
parent::init();
$this->setGeneralStuf();
}
public function setGeneralStuf() {
$this->view->oLoginForm = new Form_Login();
}
}
This kind of route definition:
routes.abc.route = "abc/buy/:id/*"
routes.abc.defaults.controller = "deal"
routes.abc.defaults.action = "buy"
routes.abc.reqs.id = "\d+"
requires a parameter in order to function. You can do this with actionStack but you can also specify a default id in case that none is provided:
$this->_helper->actionStack('Action',
'Controller',
'Route',
array('param' => 'value')
);
routes.abc.defaults.id = "1"
For Me this worked like a charm
class abcController extends Zend_Controller_Action
{
public function dashBoardAction()
{
$this->_helper->actionStack('list-User-Data', 'xyz');
}
}
class XyzController extends Zend_Controller_Action {
public function listUserDataAction()
{
$data = array('red','green','blue','yellow');
return $data;
}
}