Laravel 5: custom routing

stevendesu

I have a site that I'm building which has two different types of components: pages (static files with images and text) and forms (dynamic documents which take input). I will be creating and editing both of these via an admin panel, so there will be a Repository model and controller for each one.

It would be quite easy in Laravel to create routes like the following:

Route::match(['get', 'post'], '/pages/{one?}/{two?}/{three?}', 'PagesController@loadPage');
Route::match(['get', 'post'], '/forms/{one?}/{two?}/{three?}', 'FormsController@loadForm');

Unfortunately this would lead to some ugly URLs for people using the site:

I would prefer to remove "pages" or "forms" from these links then check the database to figure out which one to load:

I created a Path eloquent model with two properties: path and controller. So I currently have the following:

Route::match(['get', 'post'], '{one?}/{two?}/{three?}', function() {
    $path = Path::where('path', '=', implode('/', func_get_args()))->take(1)->get()->first();
    // Somehow use $path->controller to load the proper controller
});

I've tried using App::make($path->controller) but it throws the error: "Class PagesController does not exist"

I believe I can use middleware to modify the request before it reaches my app (thus pre-pending "pages/" or "forms/" accordingly without modifying the URL seen by the user) however I can't find a way to do this.

Kyobul

A simple thing to do is to check the url pattern against a database record. That's what I do with simple urls which represent complexe website structure. You can use a table which stores the url string and the url type (page, form...), each url being unique.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Laravel 5 routing using prefix

分類Dev

Laravel 5 Routing Calling Wrong View

分類Dev

Laravel 5 - where to place reusable custom validator?

分類Dev

Laravel 5 accessing custom functions in a view

分類Dev

Laravel 5 FormRequest custom message for array input

分類Dev

Custom validation rule in Laravel 5 registrar

分類Dev

Writing to custom log aswell as laravel.log in laravel 5

分類Dev

Laravel routing and route parameters

分類Dev

Laravel routing issues

分類Dev

Laravel 4 multilanguage routing

分類Dev

Laravel routing varying

分類Dev

Laravel routing not working correctly

分類Dev

Facing an issue with routing - Laravel

分類Dev

Laravel 5.5 Crud Routing

分類Dev

How can I use my custom class in a view on Laravel 5

分類Dev

Access new Config file created in custom classes in Laravel5

分類Dev

Restler custom URL routing not working

分類Dev

Routing attributes in ASP MVC 5

分類Dev

Laravel routing not working, How to fix it?

分類Dev

Laravel routing 404 error not found

分類Dev

Custom routing in Azure functions using HttpTrigger attribute

分類Dev

Routing control name and add custom markers

分類Dev

Custom value type, EF Code First and routing

分類Dev

How to add HTML5 custom data-* attribute to Laravel 4 blade template?

分類Dev

List of queryParams is empty in Angular 5 routing

分類Dev

Check if model exist and continue routing if not found in Laravel

分類Dev

Laravel-like routing pattern in Flutter

分類Dev

Laravel routing: static text in optional url segment

分類Dev

Laravel Routing to non index method in controller