user2227553
user2227553

Reputation:

Routing in laravel - error 404

I have problem with routing page and realizations. When the page routing is before realizations, page it works, realization do not work. Similarly... When the realization routing is before page routing, realization works, page do not work. Display error 404.

My routing page:

Route::get('{page}/{subs?}', ['uses' => 'PagesController@getPage'])
    ->where(['page' => '^((?!admin).)*$', 'subs' => '.*']);

My routing realizarion:

Route::group(['middleware' => ['web']], function () {
    Route::get('realizacje/{slug}', 'PagesController@getRealization')
        ->where('slug', '[\w\d\-\_]+');
});

My controller PagesController:

public function getPage($slug){
    $realizations = Realization::orderBy('id', 'desc')->get();

    $page = Page::findBySlug($slug);
    if (!$page)
    {
        abort(404, 'Please go back to our <a href="'.url('').'">homepage</a>.');
    }

    $this->data['title'] = $page->title;

    $metatag = $page->extras;
    $metatag = json_decode($metatag, true);


    $this->data['page'] = $page->withFakes();
    return view('pages.templates.'.$page->template, $this->data)
    ->with('metatag',$metatag)
    ->with('realizations',$realizations);
}

public function getRealization($slug){

    $realization = Realization::where('slug', '=', $slug)->first();
    $realizations = Realization::orderBy('id', 'desc')->get();
    return view('pages.templates.'.$realization->template)

    ->with('realizations',$realizations);

}

Upvotes: 0

Views: 658

Answers (1)

Igor W.
Igor W.

Reputation: 431

First thing: i don't know Laravel. I develop in Symfony.

But maybe it could help.

The problem could be in Your routes URIs definitions:

Route::get('{page}/{subs?}'...

Route::get('realizacje/{slug}'...

Maybe try to modify Your page URI like this so router won't match those two actions:

Route::get('pages/{page}/{subs?}'...
Route::get('realizacje/{slug}'...

Or try to modify where() conditions.

Upvotes: 2

Related Questions