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.
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]
コメントを追加