You know wordpress template system. I'm writing a simple script with Laravel. I'm very beginner at Laravel. I need to more than one template and it must be selectable at admin panel.
I created subfolders at views doc like that:
project/resources/views
defaulttemplate
othertemplate
othertemplate2
I keep template's doc name in the database when one is selected. I want to run the site selected template.
How to I can do it?
Firstly, Wordpress has a million lines of code and it's open source. You have to observe their codes from github repo if you have ever never implemented an templating engine as same as Wordpress or any kind of equivalent applications. It's not best practice however you can simply create a new namespace for the view from the app container.
You can boot your namespace definition on AppServiceProvider
app("view")->addNamespace("theme",base_path('themes/'.DEFAULT_TEMPLATE_FOLDER_NAME));
Within this way, you can pass different folders as a view. you have to call your view files via themes:: namespace.
Let's assume you have a default folder in the themes folder as a default template and there is index.blade.php inside of it. You have to call it as a return view('themes::index') in the controller. If DEFAULT_TEMPLATE_FOLDER_NAME changed, the themes:: namespace will point to changed destination due to App Service Provider. It's a simplest version of wordpress-like templating. For the best case, you have to observe different kind of open source projects.
Related
I've taken over management of a large complex site with multiple laravel installations of various ages. Some are in use some are only partially so for some routes (complex htaccess redirects abound).
I'm new to Laravel so this is a bit of a headache for me.
Is there an easy way to have laravel include the source path and filename of the controller, model and view it has used when rendering a view so I can find what files on the server are responsible for what 'pages' on the site?
TY
If you have a relative small site (i.e. a handfull of pages) you can visit each page with Laravel debugbar activated. In my office we use this for a while now and we are pretty positive about it. There is a special 'views' tab that enables you to see which views are included when you visit a certain page. It has some nice options, you can even view queries!
Example from our development environment with APP_DEBUG=true in the .env file:
In addition to the debugbar, you can also consider a package that shows route information for you by listing all routes that are called and showing the names of controllers, methods and request type in an other colour. We use Pretty routes since it is more convenient for us to read all 'routes' in the browser than in the terminal.
When starting from the app/ directory, the path of any of these classes is the same as the namespace they are in.
So if you have a model App\Models\User, this will be located in app/models/User.php.
As for the views, you start in the resources/views/ directory, replace dots in the view name by / and add .blade.php.
So, a view named website.index is located in resources/views/website/index.blade.php.
This is kind of an advanced answer that requires some Linux skills, but with the built in commando strace you can see which resources are opened: which php file, which database calls etc. This way you can analyze what is used when.
A very good tutorial can be found here:
https://hackernoon.com/debugging-a-php-application-with-strace-4d0ae59f880b
another great article on strace with PHP: https://ma.ttias.be/linux-application-script-debugging-with-strace/
I would also suggest to use all kind of filters and grep to filter the output to something meaningful.
I understand this sort of breaks the structured point of Laravel, but there is method to my madness. I plan on using a single install of laravel to host several websites that are database driven. At the moment all of the sites share the same layout and I have a system to store some custom CSS in the DB to give each site a different color scheme. I want to change this so they can use completely different views. So site A loads views/theme1/app.blade.php and site B loads views/theme2/app.blade.php.
I have implemented this by using the following to return a view.
$theme = getDomainThemeName();
return view($theme.'/home');
This is also working, but i am now left with the task of dynamically loading the assets. I am using bootstrap the generate the themes and making a few tweaks to the HTML to create the app.blade.php file. I have 2 potential solutions to this but i would much rather a way to server the css files from the views directory. This means the following mapping.
http://website.com/css/style.css =>
/resources/views/theme1/css/style.css
Can something like this be done? Another option would be to use php to read the css file and insert it into the app using a yield. It works, but it means i cant use browser caching to cache the assets. I was also thinking i could just create sub directories in the public folder. public/theme1/css/style.css. This makes the most logical sense, but it means i have to fragment the theme system. Id like to be able to unzip a theme in the views directory and it just works.
I am using Laravel 5, i have root access to the server too. Running PHP 5.4 on centos 7.
I think the best approach is having next structure:
/public/css/style.css
/public/css/theme1/custom1.css
/resources/views/common.blade.php
/resources/views/theme1/main.blade.php
And then loading conditionally with php each site theme.
It may not be any trouble with caching.
I recently finished my website's transition to Laravel and all that is left is the current forum software, vBulletin.
Right now, my website consists of various Blade files (all extending the main layout).
For the forum to work, I had to place all vBulletin files in a /forum directory inside /public and access them directly (having to add an exception in .htaccess file). So, by accessing them directly (by visiting example.com/public/forum/index.php for example), I am loosing all the Laravel functionality. But I also want to use some methods and controllers I have already defined in my Laravel site. Get the current user, his profile picture and other stuff.
Moreover, how can I use the Blade files outside of Laravel? For example, I would like to have a common header (which is already in its own header.blade.php file). How can I accomplish that instead of creating a new header.php file and have all blade syntax converted to html/php?
The question(s) above may be specific for vBulletin, but could be expanded to any application:
How do we use an external application inside Laravel, utilising Blade syntax and all the Classes / Controllers already implemented?
Thanks in advance,
Ilias
What is the best way to create a single page module for Yii2?
For example using Ember, I will have index.html and assets folder to publish.
I see two ways, one would be to just put the application under web accessible folder, it will work fine.
But what if i want to check access to the application using existing RBAC?
Another way would be to create a module and in default controller have something like
return $this->renderFile('#path/to/index.html');
And load all assets with Asset Bundle.
The problem with this approach is that i will not know the folder where assets will be loaded (it can be solved with afterCopy callback or something, but all this doesn't look nice at all).
Please advise.
Certainly it is a personal choice technique, since control RBAC is manageable level action and does not pose any problem. Once the controller is easy applicarre your organization's access control using a suitable configuration of the Access Control filter.
Alternatively, the fact of creating a module appropriately for these purposes makes it all the better organized and, precisely, modular, beyond the greater complexity in the creation of the various parts in play (module, asset, cofig / main.php) yii2 handles very well and automatically the assets and necessariio not know a priori in the name of the folder where I finish the specific assets (Yii2 find what they need).
However if this is not a 'module' with reusable application characteristics I would opt for the first solution
I have an application in PHP that is a simplified MVC type structure. This application will be used in over a dozen sites run by my company and others. The structure consists of a controller file, a folder called application which contains the app classes, and a folder called site that contains site specific code such as templates. Periodically, we distribute a patcher that updates the controller and the application folder when we are distributing changes. As a result, clients can't really alter the core code in the application folder without losing those changes when the code is updated.
I tell you that so I can explain what the clients want - they want the ability to extend existing classes. Is there a design pattern that would allow this? Right now, I have given users the ability to replace entire classes:
spl_autoload_register('loadSiteClass');
spl_autoload_register('loadCoreClass');
This uses an autoloader that checks the site folder for a class before looking in the application folder for the class. I am hoping to find an alternative that would allow me to autodetect, load and use classes that extend existing classes instead of forcing the class to be completely replaced. Any suggestions would be greatly welcomed. Thanks in advance.
If it helps, there is also a site specific config file in the site folder, where the site admin can add config settings telling me that a class is being extended, but I'm still not sure how to use that information if it were provided.
The way codeigniter does it is have a mirror directory structure in the "client" / "application" folder of the system classes. Then you can make a file in the same place as the system class, then simply extend the system class and their loading class will look for the override first and then fallback.
Check here for some code examples: https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Loader.php#L277
Their documentation may be more help to understand their pattern.
I'm sure there are more ways to do this ... but generally convention over directory structure and simple PHP "extends" keyword are what makes sense to me.