D
D
Dmitry2015-05-11 03:54:45
Laravel
Dmitry, 2015-05-11 03:54:45

How to write database-related routes in Laravel 5?

Routes:

Route::any('/', function () {
    return redirect()->route('getpage', ['index']);
});

Route::any('{requestedPage}/{requestedSubPage?}', [
    'as' => 'getpage',
    'uses' => '[email protected]'
]);

Page controller (function responsible for distributing pages):
public function getpage($requestedPage = 'index', $requestedSubPage = null)
    {
        $wantedPage = $requestedSubPage ? $requestedSubPage : $requestedPage;

        $page = Pages::findByUrlOrFail($wantedPage);
        if (is_null($page)) {
            abort(404);
        }

        $page = $page->toArray();
        if ($page['type'] == 'static') {

            return view('pages.static')->with($page);

        }
        return redirect()->action('App\Http\Controllers\[email protected]'); // Эта строка не работает, почему-то
    }

If the page type is specified as "dynamic", then you need to connect a controller for this page. redirect()->action() doesn't work and that's the problem I think.
Where is the mistake? It seems that the routes confused a little. Perhaps there is a variant of another solution with similar requirements? (Infa about pages is stored in the database).
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Plisko, 2015-05-11
@AmdY

So in your example above, you have redirect()->route('getpage', ['index']);
And when you call action, you do not pass an additional parameter, and therefore it is impossible to resolve the route. You must pass the requestedPage value and optionally requestedSubPage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question