0

I have a function in a loop:

for ($i=1; $i<=10; $i++) { Route::get('/projects/oop/'.$i, function(){ return View::make('projects.oop'. $i); <--- This line give me a problem. }); }; 

I keep getting:

i is not defined.

How will I fix it so I can have the access to my $i variable?

2
  • 3
    PHP is not Javascript. You cannot concatenate strings using + in PHP. Commented Aug 4, 2015 at 22:46
  • I fixed it. Thanks. I got confused for a bit. Commented Aug 4, 2015 at 22:52

1 Answer 1

2

This is not the proper way to handle dynamic route paths in laravel.

What you need to do is remove that nasty for loop all together, then you need to pass a dynamic value as the 3rd argument to this Route.

Route::get('/projects/oop/{id}, 'ProjectsController@oop'); 

Now, you need to go to your controllers directory and make a controller called:

class ProjectsController extends BaseController { function oop($id){ return View::make('projects.oop.'.$id); } } 

Now your route will properly pass the request to the ProjectsController class, which will then imeplement the oop function and pass the {id} from the Route to the function as an argument.

Sign up to request clarification or add additional context in comments.

2 Comments

I think that $id will always be "set" in your example wouldn't it? In my experience better to leave $id without a default value and then check is_null($is).
@JakeWilson You may be entirely right about that point, as without having an ID at the end of the route, that route would not resolve in any event. I will adjust my conditional to reflect this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.