-
I'm trying to think of the best way to add pages (similar to the Pages in Statamic: https://docs.statamic.com/pages). It seems the best way to do this is create a |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 1 reply
-
Hi @wratke! I see 3 possibles approaches there, depending on the number of pages you'll need and their forms compositions:
Most of the time for us at AREA 17, projects use a combination of 1 and 2, as well as a "generic" page model where all records share the same form but users are benefiting from the block editor to create any composition on those. |
Beta Was this translation helpful? Give feedback.
-
Thanks, this is helpful. Is it possible to use a route that points directly to a specific record in the database in the |
Beta Was this translation helpful? Give feedback.
-
You could do it using the id, yes, by using the 'page' => [
'raw' => true,
'route' => /pages/1/edit
] However, you can also register a custom route for it: Route::module('pages');
Route::name('homepage')->get('homepage', 'PageController@homepage'); Then, in the public function homepage()
{
// find the page by type or anything you've decided to use to differentiate them
abort_unless($page = Page::whereType('homepage')->first(), 500, 'Homepage missing');
// this is so that Twill always goes back to that form's route directly, not pages listing
Session::put("pages_back_link", route('admin.homepage'));
// with this approach you can provide your own view for each page
return view('admin.pages.homepage', $this->form($page->id));
} |
Beta Was this translation helpful? Give feedback.
-
Great, thanks! |
Beta Was this translation helpful? Give feedback.
-
One quick question. Is it possible to access the data that populates the form in the blade template? Is that data accessible as a PHP variable ? I can see how to create conditional logic in the blade template in the Laravel docs, but I need to get a sense of how Twill handles the data behind the scenes before I can do that. |
Beta Was this translation helpful? Give feedback.
-
I'm talking specifically about the form.blade.php template generated for a module. If I have a field in the database called |
Beta Was this translation helpful? Give feedback.
-
You should be able to access the edited model using the |
Beta Was this translation helpful? Give feedback.
-
It works! Thanks. |
Beta Was this translation helpful? Give feedback.
-
Hi @ifox I've been trying to wrap my head around how you create single pages in Twill. The above method sounds like the most logical in many cases when you want to keep your pages as separate from each other as possible. I just have a hard time figuring out what is the so to speak 'best practice' to go about it. I am new to Laravel so I tried to avoid writing obvious questions but I am slightly stuck on this one. I basically need to create a simple About page and the way I did it is that I created a CRUD module
It definitely solves my problem but the question is is this the "right" way to do it or will it cause problems down the line? Is there a more elegant way to do this? My frontend will be consumed through graphql and completely detached from the twill/laravel part of the site. Thank you! |
Beta Was this translation helpful? Give feedback.
-
I managed to get this to work really nicely with a custom approach. Firstly my migration file will have these fields
When a page is created I have an observer that runs the following:
This copies a simple stub from a stubs folder into two places
Then my frontend controller and routes to display pages looks like this
in my cms form I simply include the form blade file
Now can build custom forms for each page so long as I specify which fields are savable in the json field
|
Beta Was this translation helpful? Give feedback.
Hi @wratke!
I see 3 possibles approaches there, depending on the number of pages you'll need and their forms compositions:
A single
pages
module, with all fields and relationships in the same Eloquent model. This can seem ugly, but the simplest way to implement if you don't have tons of pages with very different fields. In the form's Blade file, you can include a different file depending on a type you store in pages records (seeded upfront or created through the UI, with a dropdown to chose type inside the Add new modal, which you can add by overwritingcreate.blade.php
in your module views).One module per page, which means a single record in each associated table, and no listing in …