I am using Route:Controller
for a laravel 5.2 project.
I found myself in an interesting situation.
Suppose My route file has this code
Route::controller('test','TestController');
TestController
has following methods
class TestController extends Controller
{
public function getIndex(){
dd('index');
}
public function getDetails($id){
return $id.'/details';
}
public function getItem($var1,$var2){
return $var1.$var2.'/item';
}
}
so if i route localhost:8000/test
it shows the content of getIndex()
method.
Now i want to browse url for this item's details
localhost::8000/test/item1/details
localhost::8000/test/item2/details
localhost::8000/test/item3/details
....
localhost::8000/test/itemN/details
I don't know how to do it when my route controller is like this
Route::controller('test','TestController');
Because all items are variables . So to get my desired result i changed my route like this:
Route::controller('test/{id}','TestController');
and now i can access these url
localhost::8000/test/item3/details
But problem is now i am unable to access localhost::8000/test
I also wanted to access following url too
localhost::8000/test/item3/shop1/details
localhost::8000/test/item3/shop2/details
So how can i solve this problem without using route::resource
I found a solution to overcome this challenge.
In my route file i add two new routes
Route::get('test/{var1}/{var2}/details','TestController@getItem');
Route::get('test/{id}/details','TestController@getDetails');
Now i can access these urls
localhost::8000/test
localhost::8000/test/item3/details
localhost::8000/test/item3/shop1/details
But found a problem. Which is , i can also access the data using following urls
localhost::8000/test/details/item3
localhost::8000/test/details/item3/shop1
So to prevent this i have change my method
public function getDetails($id){
return $id.'/details';
}
public function getItem($var1,$var2){
return $var1.$var2.'/item';
}
to
public function details($id){
return $id.'/details';
}
public function item($var1,$var2){
return $var1.$var2.'/item';
}
and routes modified as
Route::get('test/{var1}/{var2}/details','TestController@getItem');
Route::get('test/{id}/details','TestController@getDetails');
to
Route::get('test/{var1}/{var2}/details','TestController@item');
Route::get('test/{id}/details','TestController@details');
Looking for better way to solve this :)