0

I have some problems with my controller in laravel 5.4

My routes.php:

Route::group(array('domain' => '{subdomain}.site.com','as'=>'www::','middleware'=>array('web','varnish')), function() {

   Route::any('/material/{page?}/', [
      'as' => 'www_material', 'uses' => 'www\MaterialController@index'
   ]);

});

My controller:

<?php namespace App\Http\Controllers\www;

use App\Http\Controllers\Controller;
use View;
use DB;
use Illuminate\Http\Request;

class MaterialController extends Controller {

    public function index($subdomain, $page = 1, Request $request)
    {
        echo $subdomain;
        echo $page;
       //...some code
    }
}

There is no problems with url www.site.com/material/2/:

submodain = www,
page = 2

But www.site.com/material/:

Type error: Too few arguments to function App\Http\Controllers\www\MaterialController::index(), 2 passed and exactly 3 expected

I cant understand why this happend, because default value of page is 1.

Can someone help me? I cant solve this problem alone.

Thank you.

2 Answers 2

2

Your problem is the order the arguments are in the index method.

As the Request object will always be present put that first followed by $subdomain and then $page

As stated on the php website above example #5:

Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.

public function index(Request $request, $subdomain, $page = 1)
{
    echo $subdomain;
    echo $page;
   //...some code
}
Sign up to request clarification or add additional context in comments.

1 Comment

as per the PHP docs php.net/manual/en/functions.arguments.php (example #5) on order of default arguments: 'any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected'
0

Try to remove trailing slash next to {page?} mentioned below and rerun the code.

Route::any('/material/{page?}', [
  'as' => 'www_material', 'uses' => 'www\MaterialController@index'

]);

1 Comment

This shouldn't make any difference as the .htaccess file shipped with Laravel accounts for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.