Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRobert Young
11,742 PointsLaravel PHP Controllers: PHP Controllers
Hiya,
I seem to be slightly stuck on this issue: I have set up my controllers and my routing as suggested by Hampton in the 'Laravel Controllers' section. However, I keep on getting the following error message when I try to navigate to my http://laravel.dev:8000/ page.
ErrorException in TodoListController.php line 21: Undefined variable: id
Line 21 is the line of code that displays:
return View::make('todos.show')->withId($id); (See below)
The variables have been set up and defined correctly, according to the chapter. I am now on the databases section and I am trying to make progress but I cannot so this issue out!
My code is below:
TodoListController.php:
<?php namespace App\Http\Controllers;
use App\Http\Requests; use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use View;
class TodoListController extends Controller { // extends the controller from Laravel
/**
* Display a listing of the resource.
*
* @return Response
*Will list out all of our ToDo lists
*/
public function index()
{
return View::make('todos.show')->withId($id);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in a database.
*
* @return Response
*/
etc.
}
show.blade.php:
@extends('layouts.main') @section('content') <!--Anything in here, in this section, is what is going to print into our content yield-->
<h2>Show List with ID = {{{ $id }}}</h2>
@stop
routes.php:
Route::get('/', 'TodoListController@index');
Route::get('/todos', 'TodoListController@index');
Route::get('/todos/{id}', 'TodoListController@show');
Any advice would be greatly appreciated! Thanks,
Robert London, UK
1 Answer
Petros Sordinas
16,181 PointsHi Robert,
Hope you've figured this out by now since your post is 2 days old but just in case you haven't, here is your problem:
You are calling return View::make('todos.show')->withId($id); without having defined $id or accepted it as a parameter. Furthermore, if you are following along Hampton's tutorial, you shouldn't use the index method in the controller to show a specific task; you use the index method to show all tasks.
If you put return View::make('todos.show')->withId($id); in the show method, it will work because it accepts an $id as a parameter. You will have to pass the id in the url though like so: localhost.dev:8000/show/1 (where 1 is the id)
If you want the index method to show a specific id, you should modify your routes.php file from this Route::get('/', 'TodoListController@index'); To this Route::get('/{id}', 'TodoListController@index'); And add the $id parameter in your method parameters like so public function index($id)
and then enter the id in the url like so: localhost.dev:8000/1
As I said before though, you probably don't want your index method to do that.