Different folder for views - Laravel 5.4 - php

I'm trying to make a reusable Dashboard for my apps, so I decided to put everything in a folder like app/Dashboard. I've managed to have routes, controller, middlewares, etc. But, now I'd like to have views also in that folder. I added the path in config/view.php, the problem with that is that if I have two views with the same name in app/Dashboard/Views and in the default resources/views, of course the view in the default path would be loaded.
Do you have a better idea? Can I do something like return view('dashboard:login') in my controller using only the Laravel standard tools?

Look at this part of the documentation https://laravel.com/docs/5.5/packages#views. You are almost right, but you need to use two colons
return view('dashboard::login');

Related

Laravel Multiple Sites

I need to set up two sites running from the same codebase in Laravel 4.2, I have done this before but I need some slightly different requirements this time.
Some of the assets CSS, JS and images would be the same but some would need to be overridden, I am happy to just have two public folders, with all front end assets duplicated, but if there are other suggestions that could make it easier that would be great.
My other requirement is that I would like to add a new PSR include route for site 2 that could override classes from site 1, when necessary. So I could call class user as below, site 1 would call the first class, site 2 would call the second class.
app/models/user.php
app/models/site-2/user.php
Well, you could set up both domains to use same Laravel project directory and then you could create middleware which would check for current domain name:
$currentDomain = $_SERVER['SERVER_NAME'];
Then you may just check for domain and decide what routes, controllers, models, views to use right now. I think this approach will solve you needs.

Laravel localization, redirect to the right view

I'm using laravel and mcamara/laravel-localization package to manage a bilingual site (french/english).
In the package documention https://github.com/mcamara/laravel-localization
they don't explain how to point an URL to the right view depending on which language is set as local.
For example, if the URL is /en/contact load the view en.contact (en is the subdirectory for views written in english) and id the URL is /fr/contact load the view from the subdirectory fr.
The same for the URL that needs translation /en/products for english needs to load /fr/produits when the local language is set to french.
Disclaimer: I have never used the package...
That's because the package (and also the localization of laravel) assume that you are using Laravel's translations. So the package basically only worries about setting the application locale right and Laravel does the rest of the job.
However if you need different views (or don't want to use translations) you can just retrieve the current locale and use it to build the name of the view
View::make(App::getLocale().'.contact');
You have to change the view-path dynamically, so you dont have to prefix your views by hand.
Just create a new Middleware and register it in Kernel.php.
I've created a gist for that.
https://gist.github.com/vanderb/80dea9a8039dd60962c5cb291eae7b75
Now, all you have to do is to reference the view-name, without adding the lang-code every time.
I also recommend to use named-routes
https://laravel.com/docs/5.4/routing#named-routes
I don't see why you would want to use different views for each language. That would double your code. Just use one view with Laravels own translation features to translate your strings. The package handles the naming of routes (/products vs /produits). The views should stay the same.
If you really want different views for different languages, #lukasgeiter has the answer. Sounds like a code-duplication, though.
Side-note: Laravel 5.4s new JSON translation feature together with langman is really great.

Configure multiple websites using one core (CakePHP)

I have a system runing on my webserver in /my_system folder
I need to duplicate it, for 2 new websites. They'll use the SAME system, but different databases.
I need to place them in specific folders.
Like:
/my_system/client_1
/my_system/client_2
/my_system/client_3
Then I can use url prefix to guess which client I want and which database I need.
Tthe configuration was very easy, by using .htaccess and just editing some lines in database.php
But now I'm having a problem:
When I try to access /my_system/client_1/Page/1, instead of search in Pages controller, Cake tries to search the client_1 controller, which doesn't exists.
I've tried to add the client_1, client_2, client_3 to routes prefix (Configure::write('Routing.prefixes', array('client_1', 'client_2'));), but didn't worked.
So, what's the best way to work with it?
BY only using Rewrite in htaccess I can deal with it?
Obs.:
If I try to access /my_system/Page/1 I can access it normally, the only thing I need to do is work with the mentioned prefixes.
Thanks.
You'd deal with this using CakePHP's routes.
They wouldn't be prefixes because you already have a prefix of 'my_system'.
Take a look at this great yet simple article
CAKEPHP 3 – DEFINING THE CORE OUTSIDE WEBROOT
You might be able to do the same with version 2.x

Organize controllers in sub-folders

How to organize controllers under /app/controllers in sub-
folders in CakePHP? I want to create a folder like admin inside the controllers folder and I want to create some controller related to admin. If it is possible, then how can i call a controller from a sub folder?
You can use App::build() to let CakePHP know for additional packages/configurations.
App::build(array(
'Controller' => array('/path/to/controllers', '/next/path/to/controllers')
));
You need to re-think your application structure. Cake has something built in called prefix routing that you should probably be using.
This is also available in 1.x
You can't alter the CakePHP file structure "just like that". It would require serious modification of the core to achieve this, but there is almost never a good reason to do so. If you properly follow the naming conventions, everything should be easy to locate.
What you could do (that is still following conventions and comes close to what you're looking for) is create a plugin for all your admin related tasks and then you can put all that logic under app/Plugin/plugin_name/Controller instead. That way it has it's own place, although you will need to load the plugin from you main application for this to work.

What's The Proper Way To Set The Public HTML Folder Using Yii Boilerplate?

I can't find the documentation specifying how to set the public html directory. I am trying to keep all other files and folders in the site root. Since there is a frontend and backend public html folder, I don't see how this is possible.
My Site:
/site/perl/
/site/www/ <-How do you get just the 2 public folders here?
/site/.ssh
Yii Boilerplate:
runpostdeploy
yiic
/backend/
- /config
- /www/index.php
/frontend/
- /config
- /www/index.php
/common/
/console/
/tests/
Is the whole boilerplate project supposed to reside in the sites public html folder? I thought it was bad practice to do this.
*** U P D A T E ***
Spent most the day trying to figure out this problem. The closest thing I was able to find, was a comment on one of the Yii Framwork forum posts. For now, I'm running Boilerplate with subdomains like they recommend in this comment.
http://www.yiiframework.com/wiki/155/the-directory-structure-of-the-yii-project-site/#c9444
I'm not sure if this is the correct way. But it works.
You should set your site public director to frontend/www/. The idea behind Yii Boilerplate backend/www is to have separate domain for admin options (if needed), instead of having separate module. This way you'd be able to easily have separate CWebUser, Yii::app() etc. By the docs:
backend: the backend application which will be mainly used by site administrators to manage the whole system (avoiding admin modules at frontend application to avoid confusion)
I believe if you have two application, then you need two address.
You can do with sub-dir like this:
www.site-name.com -> ../frontend/www/index.php
www.site-name.com/admin -> ../backend/www/index.php
Or you can use subdomain like this:
www.site-name.com -> ../frontend/www/index.php
admin.site-name.com -> ../backend/www/index.php
It's like Wordpress, if you need enter admin page, you access through address www.site-name.com/wp-admin
But, be sure you don't use the 'admin' path in your frontend if you use this alias to backend.
Spent most the day trying to figure out this problem. The closest thing I was able to find, was a comment on one of the Yii Framwork forum posts. For now, I'm running Boilerplate with subdomains like they recommend in this comment.
http://www.yiiframework.com/wiki/155/the-directory-structure-of-the-yii-project-site/#c9444
I'm not sure if this is the correct way. But it works.

Categories