Laravel 4: passing parameters inside route without closures - php

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.

Related

Passing multiple optional parameters in a route in Laravel

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');

Passing data from a route to a controller [Laravel]

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);
}

Getting parameters from URL in Zend

In a Zend application with an example url like this one:
http://example.com/profile/423423(some id)
How do I get param from url?
I try to use:
$this->getRequest()->getParam('action');
But I get action does not exist.
then I try something like this:
protected function _initRouter()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('profile/:catid', new Zend_Controller_Router_Route('profile/:catid',
array(
'module' => 'default',
'controller' => 'profile',
'action' => 'index' // Check your action and controller
)));
}
How do I get param from url?.
You can use the getParam() methods to retrieve request variables like route, GET and POST.
Inside your controllers action you can do this.
$this->getParam('parameterName'); or $this->getRequest()->getParam('parameterName');
If you want to get the name of action and controller that matched the current route you need to use following methods.
$this->getRequest()->getActionName() and $this->getRequest()->getControllerName()
You should just use
$this->getRequest()->getParam('profile');
from your controller.
You shouldn't need to use a custom route.
Kind regards
Garry
EDIT
I have just noticed that you wish to use the profile controller. The simplest solution would be to add an id into your url like.
http://example.com/profile/id/423423
and get the profile id with
$this->getRequest()->getParam('id');

Laravel using named routes

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.

How can I access controller methods via url in Laravel?

I'n new to Laravel and I'm not quite sure of all the routing stuff yet, so I want to do things the way I'm comfortable with for the time, which is accessing a controller method via url. So I have a controller called User, in that a function called getLogin(). I want to access this via 'mydomain.com/user/login'. It doesn't currently work, so how do I do this?
While I agree with Phil Sturgeon's article that for a typical app, it's wise to make routes as obvious as possible, there are some cases where a url segment to controller method scheme might be convenient. I sometimes use Laravel for prototyping/testing some code and need a predictable url-to-method to spin off examples. You can grab the segment using Request::segment() and camelcase it using Laravel's Str::camel(). For example, with a route like:
Route::get('/lara-learn/{method}',
array(
'as' => 'lara-learn_' . Request::segment(2),
'uses' => 'LaraLearnController#' . Str::camel( Request::segment(2) )
)
);
You can visit /lara-learn/lara-config and land on the laraConfig() method:
class LaraLearnController extends BaseController {
public function laraConfig(){
return "hey from laraConfig";
}
}
If we want, we can also choose the controller dynamically from the url, say using the first segment. So we can generalize our route even more:
Route::get('/{controller}/{method}',
array(
'as' => Request::segment(1) . '_' . Request::segment(2),
'uses' => studly_case( Request::segment(1) ) . 'Controller#' . Str::camel( Request::segment(2) )
)
);
Visiting /lara-learn/lara-config should land us on the laraConfig method example above.
Laravel does not automatically map routes in controller/method fashion.
You have not posted what is in your routes.php file, but one of the simplest approaches is to do this:
Route::get('users/login', array('as' => 'login', 'uses' => 'User#getLogin'));
There are multiple approaches, though. You might consider reading the docs about routing
Route::resource('/recipes', 'recipesController#index');
you have to make route like this and u have to access it in url as
localhost/projectname/public/recipes

Categories