user9769016
user9769016

Reputation:

How to passing multiple routes in single controller in laravel

I need to pass multiple routes in single controller.

I have about us and contact us in partial view both is being retrieved from database, so I want to access them via footer. When you click it from the footer it should link to about us or contact us page. Thanks in advance for any suggestion, I will appreciate it.

Here is controller, route, view

Controller

class MainController extends Controller
{
    public function index()
    {
        $categories = Category:: All();
        $footers = Footer::with('subfooters')->get();

        return view('combine.combined', compact('categories', 'footers'));
    }

    public function foot(Request $request, $id)
    {
        $categories = Category:: All();
        $footers = Footer::with('subfooters')
            ->where('id', '=', $id)
            ->get();

        return view('combine.combined', compact('footers', 'categories'));
    }
}

routes

Route::get('/', 'MainController@foot')->name('pages.index');
Route::get('aboutus/{id}', 'MainController@foot')->name('combine.combined');
Route::get('contactus/{id}', 'MainController@foot')->name('combine.combined');

Partial footer View

@if($subfooter->id == 1)
    <a href="{{ route('combine.combined', ['id' => $subfooter->id])}}">{{$subfooter->name}}</a>
@endif

Combined-Combine Vievs

@extends('layouts.master')

@section('header')
    @include('partials.header')
@stop

@section('content')
    @include('pages.index')
    @include('pages.aboutus')
@stop

@section('footer')
    @include('partials.footer')
@stop

Project structure

.Root
..Resource
...View
....Combine
.....combined.blade.php
....Pages
.....contactus.blade.php
.....aboutus.blade.php
....Partials
.....footer.blade.php``

Upvotes: 0

Views: 1648

Answers (2)

Dimitri Mostrey
Dimitri Mostrey

Reputation: 2340

Pointing multiple routes to the same controller is not the issue as you expected correctly. The problem is here:

public function foot(Request $request, $id)

Change this to

public function foot($id, Request $request)

Upvotes: 0

Tomas Crofty
Tomas Crofty

Reputation: 187

If I understand you correctly you want to be able to have multiple routes in one controller, to do that you have to have multiple functions in the controller.

Route::get('/whatever', 'ControllerName@functionInController');

function functionInController() {
    //Will be called on /whatever
}

Essentiall the @ in the controller chooses which function within the controller so you can have one for contact us and about etc.

Upvotes: 1

Related Questions