Passing data from a route to a controller [Laravel] - php

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

Related

returning controller not found when using it as "controller#method" in laravel

I'm trying to make the resource route of steps work with id passed in the link like this "/steps/{howitwork:id}/create" and the create method looks like this:
public function create(HowItWork $how){
...
}
so before I define the resource route in the controller:
Route::get('steps/{howitwork:id}/create/', [
'as' => 'steps.create',
'uses' => 'StepController#create'
]);
what the app returning is:
Target class [StepController] does not exist.
you have add code under the resource route and try with this.
**Route::resrouce('something', SomethingController::class);
**Route::get('steps/{howitwork}/create', [StepController::class, 'create'])->as('steps.create');
I suggest you to use this syntax:
Route::get('/', [YourController::class, 'your-method'])->name('your-route-name')->middleware(['your-middleware-1', 'your-middleware-2', 'your-middleware-3']);

Laravel passing variables inside route accessing a named route

I am trying to pass 3 variables values inside an html link that uses route to access a named routed with this code:
{{$nomos->name}}
also the route function is
Route::get('gegonota/{gid?}/{cid?}/{nid?}', ['uses' => 'GegonosController#index', 'as' => 'gegonota']);
but it doesnt work right.
How can i do it.
thanks
you can try this.
{{$nomos->name}}
Just remove ? from your routes params so it will be :
Route::get('gegonota/{gid}/{cid}/{nid}', [
'uses' => 'GegonosController#index',
'as' => 'gegonota'
]);
if you want to use ? you need to provide a default value check the docs
and if you use Laravel v5.4 you need to change your route to :
Route::get('gegonota/{gid}/{cid}/{nid}', [
'uses' => 'GegonosController#index'
])->name('gegonota');

Laravel add dynamic parameter before route

I have a route like
http://localhost.fbleads.com/{dynamic-value}/auth/facebook-login
It give me error that route not found. Any solution for this
In your routes you could maybe have something like:
Route::get('{dynamic-value}/auth/facebook-login', [
'as' => 'auth.login',
'uses' => 'AuthController#login'
]);
the {dynamic_value} can be anything and be accessible as parameter in your controller method

Laravel 5 single route multiple controller method

I have a route with parameter
Route::get('forum/{ques}', "ForumQuestionsController#show");
Now I want a route something like
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
well when I hit localhost:800/forum/add I get routed to ForumQuestionsController#show instead of ForumQuestionsController#add
Well I know I can handle this in show method of ForumQuestionsController and return a different view based on the paramter. But I want it in this way.
First give this one
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
Then the following
Route::get('forum/{ques}', "ForumQuestionsController#show");
Another Method (using Regular Expression Constraints)
Route::pattern('ques', '[0-9]+');
Route::get('forum/{ques}', "ForumQuestionsController#show");
If ques is a number it will automatically go to the show method, otherwise add method
You can adjust the order of routes to solve the problem.
Place add before show , and then laravel will use the first match as route .
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
Route::get('forum/{ques}', "ForumQuestionsController#show");
I think your {ques} parameter do not get properly. You can try this:
Route::get('forum/show/{ques}', "ForumQuestionsController#show");
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
If you use any parameters in show method add parameters:
public function show($ques){
}

Laravel 5.1 - How to access dynamic URI segment in Controller and Route

Team,
I am using Laravel 5.1, In which I have to use URI segment feature same as Codeigniter
Like eg.
URL - www.example.com/user_id/user_type/user_role/....
Want to access those parameters user_id, user_type, user_role in controller and also want to manage the Route file.
Can anyone guide me how to do this in LARAVEL?
Thanks in Advance
In your routes.php file:
Route::get('user/{user_id}/{user_type}/{user_role}', ['uses' => 'UserController#index', 'as' => 'user.index']);
In your UserController.php file:
public function index($user_id, $user_type, $user_role) {
dd($user_id, $user_type, $user_role);
}
In your route.php file write this -
If you have any optional parameter then put a Question mark(?) after that, I assumed that the parameter user_role is optional.
Route::get('user/{user_id}/{user_type}/{user_role?}', [
'uses' => 'UserController#getIndex',
'as' => 'user.get.index' // You can write any unique name you want, This will be your route name.
]);
In your Controller, You can access these parameters like this -
public function getIndex($user_id, $user_type, $user_role) {
// Here your parameters will be available to use.
// Write your logic
}

Categories