I am working on a blogging application in Laravel 8.
The application supports themes. In a nutshell, theme support works like this:
In the views
directory, I have the directory containing the views of every theme.
In public\themes
I keep the assets (CSS, Javascript) of every theme.
In the SettingsController controller I have, among others, a variable named $theme_directory, which contains the name of the directory of the current theme. I use this variable in the theme view, like this, for instance:
<link href="{{ asset('themes/' . $theme_directory . '/css/clean-blog.min.css') }}" rel="stylesheet">
This is the SettingsController:
class SettingsController extends Controller {
private $rules = [
'site_name' => 'required|string|max:190',
'tagline' => 'required|string|max:190',
'owner_name' => 'required|string|max:190',
'owner_email' => 'required|email|max:190',
'twitter' => 'required|string|max:190',
'facebook' => 'required|string|max:190',
'instagram' => 'required|string|max:190',
'theme_directory' => 'required|string|max:190',
];
private $messages = [
'site_name.required' => 'A site title is required',
'tagline.required' => 'A site tag line is required',
'owner_name.required' => 'Please provide a site owner/company name',
'owner_email.required' => 'A valid email is required',
'owner_email.email' => 'The email you provided is not valid',
'twitter.required' => 'Please provide a Twitter account',
'facebook.required' => 'Please provide a Facebook account',
'instagram.required' => 'Please provide an Instagram account',
'theme_directory.required' => 'Please pick a theme',
];
public function index() {
$settings = Settings::first();
return view('dashboard/settings', ['settings' => $settings]);
}
public function update(Request $request) {
$validator = Validator::make($request->all(), $this->rules, $this->messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator->errors())->withInput();
} else {
$settings = Settings::first();
$settings->site_name = $request->get('site_name');
$settings->tagline = $request->get('tagline');
$settings->owner_name = $request->get('owner_name');
$settings->owner_email = $request->get('owner_email');
$settings->twitter = $request->get('twitter');
$settings->facebook = $request->get('facebook');
$settings->instagram = $request->get('instagram');
$settings->theme_directory = $request->get('theme_directory');
$settings->is_cookieconsent = $request->get('is_cookieconsent') == 'on' ? 1 : 0;
$settings->is_infinitescroll = $request->get('is_infinitescroll') == 'on' ? 1 : 0;
$settings->save();
return redirect()->route('dashboard.settings')->with('success', 'The settings were updated!');
}
}
}
From the "Settings" section I can set the current theme.
At this time, I can only write the theme's directory name by hand. I wish I could pick it from a list of themes. I do not have any clear idea on how to do that (since I am not a very experienced PHP developer).
What is a robust way to "read" theme (directory) names from a list?
Hello,
You can use the Laravel Storage
facade to list all the directories inside your themes folder. The link to the official documentation is Get All Directories Within A Directory
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community