Laravel 5.6 Function () does not exist in route/web.php - laravel

This is my code using to send an email
Route::post('/mail/send', [
'EmailController#send',
]);
in EmailController this is the send action
public function send(Request $request)
{
$data = $request->all();
$data['email'] = Input::get('email');
$data['name'] = Input::get('name');
$obj = new \stdClass();
$obj->attr = 'Hello';
Mail::to("dev#mail.com")->send(new WelcomeEmail($obj));
}
getting a error as Function () does not exist

In your route/web.php file
Change it to
Route::post('/mail/send', 'EmailController#send');
Refer to the documentation to see the possible options to define routes:
https://laravel.com/docs/5.6/routing

Route's action method can be defined using a array, but not simply wrap controller#action in an array, you should assign it to array's key 'uses'.
In your example, it should be like this:
Route::post('/mail/send', [
'uses' => 'EmailController#send',
//'middleware' => .... assign a middleware to this route, if needed
]);
the array form usually is used when we want to specify more specification about the route like use a specific middleware and pass middleware parameters.
if you just want to define route's processing method you can simply use controller#action as Route::post's second parameter:
Route::post('/mail/send','EmailController#send');

In your route ...
Route::post('/mail/send','EmailController#send')->name('send_email');
Inside of your HTML form add below code...
<form action="{{route('send_email')}}" method="post">
...
{{csrf_field()}}

Related

How to use parameter from function to create an URL? Laravel Routing

I'm sending an URL hashed and when i get it i have to show a view on Laravel, so i have those functions on the controller and also some routes:
This are my routes:
Route::post('/sendLink', 'Payment\PaymentController#getPaymentLink');
Route::get('/payment?hash={link}', 'Payment\PaymentController#show');
And this are the functions i have on my controller:
public function getPaymentLink (Request $request){
$budgetId = $request['url.com/payment/payment?hash'];
$link = Crypt::decryptString($budgetId);
Log::debug($link);
//here to the show view i wanna send the link with the id hashed, thats why i dont call show($link)
$view = $this->show($budgetId);
}
public function show($link) {
$config = [
'base_uri' => config('payment.base_uri'), ];
$client = new Client($config);
$banking_entity = $client->get('url')->getBody()->getContents();
$array = json_decode($banking_entity, true);
return view('payment.payment-data')->with('banking_entity', $array);
}
And this is getting a "Page not found" message error.
What i want to to is that when i the client clicks on the link i send him that has this format "url.com/payment/payment?hash=fjadshkfjahsdkfhasdkjha", trigger the getPaymentLink function so i can get de decrypt from that hash and also show him the view .
there is no need to ?hash={link} in get route
it's query params and it will received with $request
like:
$request->hash
// or
$request->get('hash')
You need to define route like this:
Route::get('/payment/{hash}', 'Payment\PaymentController#show');
You can now simply use it in your Controller method like below:
<?php
public function getPaymentLink (Request $request,$hash){
$budgetId = $hash;
// further code goes here
}

Pass function variable INSIDE route declaration

Let's say I have a function in my controller which retrieves users looking something like this:
public function index($category) {
// retrieve users depending on category or all
}
Now is there a way to make named routes to include the function parameter like so:
Route::get('passengers', 'Controller#index(1)')->name('passengers');
Route::get('attendees', 'Controller#index(2)')->name('attendees');
This way they can all use the same function
No you can not pass a parameter the action name, and there is a problem in you routing logic :
Route::get('/{categoryName}', 'Controller#index')->name('index');
And in the controller you will for example get the category by name like this :
public function index($categoryName) {
$category = Category::where('name', $categoryName)->first();
// use $ category as you please ;)
}
In the blade :
route('index', ['categoryName' => $category->name])
If the named route defines parameters, you may pass the parameters as the second argument to the route function. The given parameters will automatically be inserted into the URL in their correct positions
https://laravel.com/docs/5.5/routing#named-routes
So, use route() helper like this:
route('passengers', ['category' => 1])
Then you need to add {category} to the route. Also, it's really better to use show() instead of index() here. So, your route will look like this:
Route::get('passengers/{category}', ['as' => 'passengers', 'uses' => 'Controller#show']);
Yes, you can define the param in the url like so:
Route::get('passengers/{yourParam}', 'Controller#index')->name('passengers');
View in docs
Route::get( '{category}', [ 'as' => 'users', 'uses' => 'Controller#index' ]);
Remember to add this route at the end of your routes file in order to not to collide with any other route.
Now in your controller
use Illuminate\Http\Request;
public function index(Request $request)
{
$category = $request->query('category');
// $category will be passengers, attendees, etc
}
Your routes will be
/passengers can be accessed as route('users', ['category' => 'passengers'])
/attendees can be accessed as `route('users', ['category' => 'attendees'])

Passing variables to replace wildcard on laravel Route?

I'm middle of practice laravel , basic lesson 11th on laracast, wondering that if I create an entity from form page like below
<html> blahblah..
..
<form method="post" action="{{ Route('customModel.store') }}">
forms.. many forms..
</form>
..
</html>
When I submit this form, data will flow through the router.
Route::post('/customModel', [
'as'=>'customModel.store',
'uses'=>'CustomModelController#store
]);
The CustomModelController has its method named store and problem is here..
public function store( Request $request )
{
$CustomModel = CustomModel::create([
'name' => Request('name'),
'desc' => Request('desc')
]);
// Here is the PROBLEMMMM..
return redirect('/field/'. $CustomModel->id );
}
It feels really... mm... weird using redirect function directly and attach some variables directly to fill wildcard value.
Is there other ways to replace redirect()?
Something like do something with Route or another?
You can also use route() method of Illuminate\Routing\Redirector class as:
return redirect()->route('route_name', ['id' => $id]);

Passing parameter from route to controller in laravel

I am trying to pass a variable from route to controller. But not able to succeed.
my route entry is as below
Route::get('/Register', 'NewRegister#CheckCand');
now in the controller file I want to get one parameter. My controller function is as below
public function CheckCand()
{
echo $ID;
}
Now how do I pass a variable ID from route to controller. But i don't want to pass it in the URL and also in get function i dont want to change '/Register'.
Route::get('/Register', 'NewRegister#CheckCand');
Means it will be like a Hidden parameter passed from route to controller.
Might be question is confusing but i don't know how to explain better.
You can retrieve the parameters by Request so in your function request the parameters as you named when you passed them
public function CheckCand(){
$request = Request::all();
$ID = $request->ID ;
// Or
$ID = $request['ID'];
}
Pass as request parameter and retrieve it in the controller using request. Something like below.
jQuery
$.get('/register', {ID :'123'}, function(data) {});
or
Javascript
...
<form id="register-form" action="{{ url('register') }}" method="GET" style="display: none;"><input type="hidden" name="ID" value="..."/></form>
Controller
public function CheckCand(Request $request) {
$request->ID;
}

Laravel - Change URL parameters using GET

I have RESTful API built on Laravel.
Now I'm passing parameter like
http://www.compute.com/api/GetAPI/1/1
but I want to pass parameter like
http://www.compute.com/api/GetAPI?id=1&page_no=1
Is there a way to change Laravel routes/functions to support this?
you can use link_to_route() and link_to_action() methods too.
(source)
link_to_route take three parameters (name, title and parameters). you can use it like following:
link_to_route('api.GetAPI', 'get api', [
'page_no' => $page_no,
'id' => $id
]);
If you want to use an action, link_to_action() is very similar but it uses action name instead of route.
link_to_action('ApiController#getApi', 'get api', [
'page_no' => $page_no,
'id' => $id
]);
href text
with these methods anything after the expected number of parameters is exceeded, the remaining arguments will be added as a query string.
Or you can use traditional concatination like following:
create a route in routes.php
Route::get('api/GetAPI', [
'as' => 'get_api', 'uses' => 'ApiController#getApi'
]);
while using it append query string like this. you can use route method to get url for required method in controller. I prefer action method.
$url = action('ApiController#getApi'). '?id=1&page_no=1';
and in your controller access these variables by following methods.
public function getApi(Request $request) {
if($request->has('page_no')){
$page = $request->input('page_no');
}
// ...your stuff
}
Or by Input Class
public function getApi() {
if(Input::get('page_no')){
$page = Input::get('page_no');
}
// ...your stuff
}
Yes you can use those parameters, then in your controllers you can get their values using the Request object.
public function index(Request $request) {
if($request->has('page_no')){
$page = $request->input('page_no');
}
// ...
}

Resources