I am using lumen 7 framework.
I had a bug. In the web.php file I put:
$router->get('/getAll/{param1:[0-9]+|2A|2B}/{param2:[0-9]+}', 'TestController#getAll');
So, in the TestController, I create the function like that :
public function getAll($param1, $parm2)
{
....
}
The isssue is :
Illuminate\Contracts\Container\BindingResolutionException: Unable to resolve dependency [Parameter #1 [ $param2 ]] in class App\Http\Controllers\TestController
I did some tests, I add this bloc to provider, but it didn't work.
$this->app->singleton(\Illuminate\Contracts\Routing\ResponseFactory::class, function() {
return new \Laravel\Lumen\Http\ResponseFactory();
});
Also, I checked if I had done something wrong in the web.php file but I changed the function in the controllers to :
public function getAll($param1)
{
$parm2=1;
....
}
and it works fine.
How can I fix this bug, because in the url I need the two params.
Thanks,
The issue was that the names in controller and in the route was not the same.
The names in the controller was a little different from the route.
I did a upgrade from lumen 5.6 to lumen 7.0 and I thinks since Lumen 5.8 the names must be the same.
In route, you're passing only one param that's why you're getting this error.
$router->get('/getAll/{param1:[0-9]+|2A|2B}/{param2:[0-9]+}/{param2:[0-9]+|2A|2B}/{param2:[0-9]+}', 'TestController#getAll');
In controller.
public function getAll($param1, $param2)
{
....
}
Related
I am running into the following problem, I want to pass from one livewire controller a series of variables to another livewire controller which is in another view.
From what I've read the most efficient method would be to pass the parameter through the route (Route Model Binding), and receive it via the mount() method in the livewire controller.
But when receiving the parameter from the livewire controller, I get the following error. (Even though the parameter appears in the route)
Unable to resolve dependency [Parameter #0 [ <required> $prioridad ]] in class App\Http\Livewire\ResultadoConsultaPresupuesto
Controller that redirects to the required route by passing the variable
$this->validate($validationMoto);
return redirect()->route('resultado-presupuesto', ['prioridad' => $this->Prioridad]);
Second controller (receives the parameter)
class ResultadoConsultaPresupuesto extends Component{
public $Prioridad;
public function mount($prioridad){
$this->Prioridad = $prioridad;
}
public function render()
{
return view('livewire.resultado-consulta-presupuesto');
}
}
Route (web.php)
Route::get('/resultado-presupuesto/{prioridad}', [PageController::class, 'showResultadosPresupuesto'])->name('resultado-presupuesto');
I have already made
php artisan route:clear && php artisan route:cache
If instead of receiving the parameter in the mount function I receive it in the following way if it works, but I don't understand why the first one doesn't work
public function mount(){
$prioridad = \Route::current()->parameter('prioridad');
$this->Prioridad = $prioridad;
}
Looking at this you are using standard Laravel controller. So you should pass variable to Livewire component in your view
Something like:
<livewire.resultado-consulta-presupuesto :prioridad="$proridad" />
and before you need to set it it controller
In Laravel 7 fetching a model was pretty straightforward, i just needed to setup mi resource route and make a get to the address:
http://localhost/test/public/employee/1
But i cant make it work on Laravel 8, according to my understanding i just need to do this:
public function show(Employee $employee)
{
dd($employee);
}
But dd only returns an empty class:
If i do this:
public function show(Employee $employee)
{
dd(Employee::find(1));
}
dd returns the correct data:
Route::resources([
'employee' => EmployeeController::class,
]);
Can somebody help me find what am i missing?
Regards...
Route::resource('employee', EmployeeController::class);
https://laravel.com/docs/8.x/controllers#resource-controllers
Your Route must have the same variable like
Route::get('/employee/{employee}', 'EmployeeController#show');
And ensure you have binding middleware enabled for this route. ->middleware(['bindings']);
Updating for Resource routing:
Route::resource('/employee', 'EmployeeController')->middleware('bindings');
The problem was that i was naming the routes in spanish:
Route::apiResource('empleados', EmployeeController::class);
And because of this Laravel is expecting to receive the model encapsulated in a spanish verb class (empleados instead of employee), So i needed to rename the parameter inside the method controllers to receive the correct model:
public function show(Employee $empleado)
{
return $empleado;
}
I have a route setup which is throwing a 404 in my Laravel 5.6 app.
The problematic route is:
Route::get('/project/{project_id}/issue/create', 'IssueController#create');
If I remove the {project_id} parameter the view loads..but I need to be able to pass this id since I will be using it on this view to create new issues that are assigned to a project. All of the other routes work without issue.
My routes file (web.php) looks like this:
Route::get('/projects', 'ProjectController#index');
Route::get('/project/{project_id}', 'ProjectController#show');
Route::get('/project/{project_id}/issue/{issue_id}', 'IssueController#show');
Route::get('/project/{project_id}/issue/create', 'IssueController#create');
And my create function in the IssueController file is this:
public function create()
{
return view('issue.create');
}
You missed project_id as parameter of your create method. Try this:
public function create($project_id)
{
return view('issue.create');
}
and make a route like this:
Route::get('/project/issue/create/{project_id}','IssueController#create');
Any help why this is not working ,I am using Laravel 5.5.23 version ,this is are my routes :
<?php
Route::get('/', function () {
return view('welcome');
});
Route::resource('threads','ThreadController');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('threads','ThreadController#index');
Route::get('threads/{channel}','ThreadController#index');
Route::get('threads/create','ThreadController#create');
Route::get('threads/{channel}/{thread}','ThreadController#show');
Route::post('threads','ThreadController#store');
Route::post('/threads/{channel}/{thread}/replies','ReplyController#store');
Route::get('/logout' , 'Auth\LoginController#logout');
This is the ThreadController ,just the relevant methods actually :
public function __construct()
{
$this->middleware('auth')->except(['index','show']);
}
public function index($channel = null)
{
if($channel){
//do something
}
else{
$threads=Thread::latest()->get();
}
return view('threads.index',compact('threads'));
}
The problem is when I try to access /threads/someChannel it returns not found ,so this is the problematic route : Route::get('threads/{channel}','ThreadController#index'); ,all other routes are working ,any idea why this one is not working ?
The Route::resource('threads','ThreadController') call is defining 7 routes with the prefix threads. Some of the routes you are defining yourself are being masked by this. Check php artisan route:list to see what routes the Route::resource call registers for you first. First come first serve when it comes to matching routes.
/threads/someChannel is going to match the route defined by the resource call:
GET /threads/{thread} ThreadController#show
Since you don't have the definition for show which is relevant to have, I would assume you have Implicit Model Binding happening. It is trying to bind that model to that parameter and it can't find it in the database by that ID and is causing a 404 because of it.
I use laravel 4 and am not able to show a component :
this is a line from my routes
Route::resource('/', 'PostsController');
and this is my show function from PostsController.php
public function show($id) {
return "HI";
}
And This is the line that links to the function from my view
<h1>{{$post['title']}}</h1>
And It properly links to localhost:8000/show/1
But I'm amazingly getting a Not found HTTP exception from laravel.
How do I get this to work?
So the answer is that #NihalSahu is wrong (me) and that resourceful routing doesn't work like that it would be infinitely better so as to set the router to host/{id}