I have an a tag within a form that passes multiple optional parameters to the route. But when I skip one parameter in the middle and pass one after that it gives page not found.
How can I overcome this issue without using a form.
Here is my a tag:
Download
My route:
Route::group(['prefix'=>'/employee','as'=>'employee.'], function(){
Route::get('/', [EmployeeController::class, 'index'])->name('index');
Route::get('/employee-list/excel-export/{start_date?}/{end_date?}/{empId?}/{name?}', [EmployeeController::class, 'employeeListExcelExport'])->name('employeeListExcelExport');
});
The reason I can't use form is because this tag is inside a form and it's not idea to use nested forms.
Change your route like this (clear optional parameters that you add to path):
Route::get('/employee-list/excel-export', [EmployeeController::class, 'employeeListExcelExport'])->name('employeeListExcelExport');
Now route helper method will generate a URL with query parameters, for example:
route('admin.employee.employeeListExcelExport', ['start_date' => $start_date, 'end_date' => $end_date, 'empId' => $empId, 'name' => $name] ) }}
// Returns 'http://localhost/employee/employee-list/excel-export?start_date=2022-09-12&end_date=2022-10-11&empId=3&name=erkan'
And now you can directly reach your optional parameters inside employeeListExcelExport method in EmployeeController
request()->get('start_date') or $request->get('start_date');
Related
I do not know how to use this in order to always have GET parameters
Route is setup like
Route::controller('/test', TestController::class);
If I specify the getIndex function, it adds GET parameters
>>> action('TestController#getIndex', ['type' => '123'])
=> "http://localhost/test?type=123"
But if I specify anything but the getIndex function it does not add the parameters as GET variables
>>> action('TestController#getSuccess', ['type' => '123'])
=> "http://localhost/test/success/123"
Take a look at the output of php artisan routes --path=/test
Some of the routes (like the one mapped to getSuccess) have parameters and Laravel attempts to match the given parameter array to the route parameters.
Once all the query parameters are accounted for, any excess elements are converted into a query string and appended to the final URL.
I would like to pass a variable from my routes file to the controller specified. Not using parameters as the information is not in the URL. I can't figure out a way to pass it using the below code.
Route::get('faqs', [
'as' => 'thing',
'uses' => 'Controller#method',
]);
I know you can redirect to a controller as well but the error says that the method does not exist and after searching I found that the controller had to be assigned to a route and after that it was still the same error but in a different location.
Any thoughts?
One way this can be achieved is by creating custom middleware by adding your custom fields to the attributes property.
So you would do;
$request->attributes->add(['customVariable' => 'myValue']);
You can use Route::bind to assign value for specific slug
Route::bind('parameter', function($parameter)
{
return SomeModel::where('name',$parameter)->first();
});
Route::get('faqs/{parameter}', [
'as' => 'thing',
'uses' => 'Controller#method',
]);
And in your controller set this as a method parameter
public function method(SomeModel $model)
{
dd($model);
}
I'm new to Laravel, and I'm trying to generate URLs using named routes, but I can't find any documentation pertaining to this scenario.. I want to generate URLs to the default authentication based routes that Laravel ships with, but coming from Silex I really dislike the idea of generating URLs using the url function and specifying the path.. I like using a bound name that I give the route (here are some examples from silex), is there any way to specify a name (or is there a dynamic name I can use) to generate the URLs for routes defined using Route::controller or Route::controllers? For example, what would I pass to route in my template to generate the logout url?
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Would I just have to dig through the traits and manually specify each controller method if I want to do this?
You can set the names for different controller actions when using Route::controller:
Route::controller('auth', 'Auth\AuthController', [
'getLogin' => 'auth.login',
'getLogout' => 'auth.logout',
// and so on
]);
However you may also use the action() helper instead of route() or url(). It let's you specify the controller and method you want to generate an URL for:
action('Auth\AuthController#getLogin')
You can set pass your route names as an array in the 3rd argument to controller:
Route::controller('auth', 'Auth\AuthController', [
'getLogin' => 'auth.login',
]);
There's no way to mass assign them.
I have a controller which I want to use for many routes. I need to pass some parameters, but I don't know how to do that without using closures.
I have an action like this:
public function show($view, $param)
{
return View::make($view)->with('param', $param);
}
Now I know I can generate a route like this:
Route::get('/myfirstlink', array('uses' => 'MyController#show') );
but I want to pass $view and $param without passing them in the url.
Something like:
Route::get('/myfirstlink', array('uses' => 'MyController#show') ); //with $view='firsttemplate',$param='firstparam'
Route::get('/mysecondlink', array('uses' => 'MyController#show') ); //with $view='secondtemplate',$param='secondparam'
How to do that in the cleanest way?
Thank you in advance
Edit for clarification:
I don't need the user to specify values. I want to call the same controller action with different parameters... something like this:
Route::get('/myfirstlink', array('uses' => 'MyController#show', 'atts' => array('view'=>'firsttemplate','param'=>'firstparam')) );
You can create an session, or send via input hidden, or a cookie.
But in your case i recommend use sessions, you can destroy/change/create it anytime.
Regarding the use of named routes, these 2 lines allow me to access the same page so which is correct?
// Named route
Route::get('test/apples', array('as'=>'apples', 'uses'=>'TestController#getApples'));
// Much simpler
Route::get('apples', 'TestController#getApples');
Is there any reason I should be using named routes if the latter is shorter and less prone to errors?
Named routes are better, Why ?
It's always better to use a named route because insstsead of using the url you may use the name to refer the route, for example:
return Redirect::to('an/url');
Now above code will work but if you would use this:
return Redirect::route('routename');
Then it'll generate the url on the fly so, if you even change the url your code won't be broken. For example, check your route:
Route::get('apples', 'TestController#getApples');
Route::get('apples', array('as' => 'apples.show', 'uses' => 'TestController#getApples'));
Both routes are same but one without name so to use the route without name you have to depend on the url, for example:
return Redirect::to('apples');
But same thing you may do using the route name if your route contains a name, for example:
return Redirect::route('apples.show');
In this case, you may change the url from apples to somethingelse but still your Redirect will work without changing the code.
The only advantage is it is easier to link to, and you can change the URL without going through and changing all of its references. For example, with named routes you can do stuff like this:
URL::route('apples');
Redirect::route('apples');
Form::open(array('route' => 'apples'));
Then, if you update your route, all of your URLs will be updated:
// from
Route::get('test/apples', array('as'=>'apples', 'uses'=>'TestController#getApples'));
// to
Route::get('new/apples', array('as'=>'apples', 'uses'=>'TestController#getApples'));
Another benefit is logically creating a URL with a lot parameters. This allows you to be a lot more dynamic with your URL generation, so something like:
Route::get('search/{category}/{query}', array(
'as' => 'search',
'uses' => 'SearchController#find',
));
$parameters = array(
'category' => 'articles',
'query' => 'apples',
);
echo URL::route('search', $parameters);
// http://domain.com/search/articles/apples
The only reason to name the route is if you need to reference it later. IE: from your page in a view or something, check whether you are in that route.