Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Route pattern "/cpages/ads/{type}/{{type}}" cannot reference variable name "type" more than once

Hello,

I get this error : Route pattern "/cpages/ads/{type}/{{type}}" cannot reference variable name "type" more than once.

When trying to open this link from this menu:

       <li class="nav-item">
        <a href="{{ url('cpages/users') }}" class="nav-link">
          <i class="nav-icon fas fa-user"></i>
          <p>
            Users
          </p>
        </a>
      </li>

ref: https://laracasts.com/discuss/channels/laravel/route-pattern-usernameusername-cannot-reference-variable-name-username-more-than-once

I still do not see the connection between the menu I clicked and the error, any idea why?

0 likes
15 replies
davy_yg's avatar
Level 27
	// Advertisement
	Route::resource('cpages/ads/{type}', 'AdsController');

	Route::get('cpages/users', 'UsersController@index');

	Route::get('cpages/settings', 'SettingsController@index');
SilenceBringer's avatar

@davy_yg I'm not sure... but I think routes use last param name in singular as parameter in resource. So, possible, try to change type to types?

Route::resource('cpages/ads/{type}', 'AdsController');

so, your resource routes will looks like

 "/cpages/ads/{types}/{{type}}"
Snapey's avatar

it's probably an error on the page you are loading, not the menu link you clicked on

davy_yg's avatar
Level 27

Looks like the error comes from this routes:

web.php

// Advertisement
Route::resource('cpages/ads/{type}', 'AdsController')->parameters([
	'{type}' => 'optional?'
]);

Is it because it is not passing any id? lol. (that's why I have to make it optional?)

MichalOravec's avatar

Create your routes manually, it means don't use Route::resource in this case, that's it.

davy_yg's avatar
Level 27

This is actually works! Eventhough I don't really understand why that happens?

web.php

Route::resource('cpages/ads/{type}', 'AdsController')->parameters([
 	'{type}' => 'optional?'
]);

If I changed it to this : Route::post('cpages/ads/{type}', 'AdsController');

It gives me this error message:

 UnexpectedValueException
 Invalid route action: [App\Http\Controllers\AdsController].
 https://localhost/axe-backend/public/cpages/ads/slider
  Hide solutions
  `App\Http\Controllers\AdsController` is not invokable.

 The controller class App\Http\Controllers\AdsController is not invokable. Did you forget to add the __invoke       method or is the controller's method missing in your routes file?

ref: https://stackoverflow.com/questions/57501421/laravel-not-picking-up-invoke-method

Why I cannot use resources?

AdsController.php

	<?php

	namespace App\Http\Controllers;

	use Illuminate\Http\Request;
	use App\Advertisement;
	use Illuminate\Support\Str;
	use Session;

	class AdsController extends Controller
	{
		/**
 		* Display a listing of the resource.
 		*
 		* @return \Illuminate\Http\Response
 		*/

		public function index($type)
		{
    		//
    		return view('admin.advertisement.ads', compact('type'));
		}

	    /**
 		* Show the form for creating a new resource.
 		*
 		* @return \Illuminate\Http\Response
 		*/

		public function create()
		{
   	 //
		}

		/**
 		* Store a newly created resource in storage.
 		*
 		* @param  \Illuminate\Http\Request  $request
 		* @return \Illuminate\Http\Response
 		*/

		public function store(Request $request, $type)
		{
    	//
    		$this->validate($request, [
        	'ad_title' => 'required',
        	'ad_caption' => 'required',
        	'ad_url' => 'required',
        	'images' => 'required'
    	]);


    	if($type == "slider") {
         $dir_location = 'slider'; 
         $dir = 'images/slides/';  
    	}elseif($type == "featured_index") {
         $dir_location = 'featured_index';
         $dir = 'images/featured_index/';   
    }elseif($type == "catalog_ads") {
         $dir_location = 'catalog_ads'; 
         $dir = 'images/catalog_ads/';  
    }elseif($type == "banner_ads") {
         $dir_location = 'banner_ads';
         $dir = 'images/banner_ads/';   
    }


    $ads = new Advertisement;

    $ads->ad_type = $type;
    $ads->ad_title = $request->ad_title;
    $ads->ad_caption = $request->ad_caption;
    $ads->ad_url = $request->ad_url;
        
        // Mengelola nama

        $extension = $request->file('images')->extension();
        
        $name_wo_extension = pathinfo($request->file('images')->getClientOriginalName(), PATHINFO_FILENAME);

        $slug = Str::slug($name_wo_extension, '-');

        $name = $slug . '.' . $extension;

        // check the existance of old file
        $count = count(Advertisement::where('ad_img_url', $dir.$name)->get());

        while($count != 0)
            {
                $slug = $slug . '-copy';
            }    

    $ads->ad_img_url = $dir . $slug;
            
    $request->file('images')->storeAs($name, $dir_location);    

    $ads->save();

    Session::flash('flash', 'Ads type :'. $type . ' created');


    return redirect()->back();
	}

	/**
 	* Display the specified resource.
 	*
 	* @param  int  $id
 	* @return \Illuminate\Http\Response
 	*/

	public function show($id)
	{
    	//
	}

	/**
 	* Show the form for editing the specified resource.
 	*
 	* @param  int  $id
 	* @return \Illuminate\Http\Response
 	*/

	public function edit($id)
	{
    //
	}

	/**
 	* Update the specified resource in storage.
 	*
 	* @param  \Illuminate\Http\Request  $request
 	* @param  int  $id
 	* @return \Illuminate\Http\Response
 	*/

	public function update(Request $request, $id)
	{
    //
	}

	/**
 	* Remove the specified resource from storage.
 	*
 	* @param  int  $id
 	* @return \Illuminate\Http\Response
 	*/

	public function destroy($id)
	{
    //
	}
	}

I am afraid if I am going to need these three: index, store, & destroy from AdsController

MichalOravec's avatar
Route::post('cpages/ads/{type}', 'AdsController@store'); // @store
davy_yg's avatar
Level 27

This is actually works -

Route::resource('cpages/ads/{type}', 'AdsController')->parameters([
	'{type}' => 'optional?'
]);

Does this means giving the option whether to pass the variable or not?

Will using this solution only be okay?

davy_yg's avatar
Level 27
Route::get('/user/{name?}', function ($name = 'John') {
	return $name;
});

I don't really understand what this means? variable name is optional right? if it is empty then what is John for?

MichalOravec's avatar

When it's empty the variable $name will be John.

www.example.com/user // $name is John

www.example.com/user/david // $name is david
davy_yg's avatar
Level 27

web.php

	Route::get('/user/{name?}', function ($name = 'John') {
		return $name;
	});

Looks like this one has no controller.

Please or to participate in this conversation.