`I'm kind of new to laravel, I have a project that I made modular using nWidart/laravel-modules.
I need to use the same module in more than one project.
in the following example "Module1" is used in both Project1, Project2 and Project3. My mass structure would be close with the following:
PROJECTS
|___ Project1
|___ Project2
|___ Project3
|___ Module1
I would like to keep this structure as it follows the company's standard.
I tried some changes in my modules.php but without success:
'namespace' => '',
'paths' => [
'modules' => base_path('../..')
]
I got the following return:
PS C:\\1.PHP\\PROJECTS\\PROJECT\> php artisan module:list
Error
Class '\Teste\Providers\TesteServiceProvider' not found
at C:\1.PHP\GLOBAL\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php:208
204▕ * #return \Illuminate\Support\ServiceProvider
205▕ */
206▕ public function createProvider($provider)
207▕ {
➜ 208▕ return new $provider($this->app);
209▕ }
210▕ }
211▕
1 C:\1.PHP\GLOBAL\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php:144
Illuminate\Foundation\ProviderRepository::createProvider("\Teste\Providers\TesteServiceProvider")
2 C:\1.PHP\GLOBAL\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php:61
Illuminate\Foundation\ProviderRepository::compileManifest()
If I understand correctly, it seems like you want to use a module in multiple projects so that you don't need to duplicate the code within each module.
In order to achieve this you could create your own git repository called my-module for example and install this via composer within each project.
There's an article in the Laravel Modules documentation which highlights how to do this: https://docs.laravelmodules.com/v9/publishing-modules
The article also explains how to install private packages in case you don't want your source code to be public.
Related
I would like to run 2 Laravel 8 apps. Both should have their own packages, providers, controllers & cache, but Commands, Models, Exception, .env & Conifg should use the same.
I have the following structure:
/main/ <- Laravel Main Package with Models, etc
/sub/ <- 2nd Laravel Package
/.env
The composer.json of the sub app:
"autoload": {
"psr-4": {
"App\\Console\\": "./../main/app/Console/",
"App\\Exceptions\\": "./../main/app/Exceptions/",
"App\\Http\\": "app/Http/",
"App\\Models\\": "./../main/app/Models/",
"App\\Providers\\": "app/Providers/"
}
},
My bootstrap\app.php:
//...
if (!class_exists('Application')) {
class Application extends OriginApplication
{
/**
* Get the path to the application configuration files.
*
* #param string $path Optionally, a path to append to the config path
* #return string
*/
public function configPath($path = '')
{
return $this->basePath.DIRECTORY_SEPARATOR.'../main/config'.($path ? DIRECTORY_SEPARATOR.$path : $path);
}
}
}
//...
$app->useEnvironmentPath(
dirname(__DIR__, 2)
);
It works also so far that the .env is taken correctly, but with the classes from the main are not loaded, it comes Undefined namespace 'Console' and Unable to detect application namespace.
Does anyone know how I can best implement my plan?
I created Symlinks.
For example
sub/app/Console/ Symlink to main/app/Console/
And now the project in the folder sub use the app/Console/ files of the project in the folder main.
For my Laravel 5.5 project I used filemanager package (elfinder-laravel ) with published and adapted blade views. After awhile I found that default views are used from package folder:
/vendor/barryvdh/laravel-elfinder/resources/views
instead of published views:
/resources/views/vendor/elfinder
I tried to republish views, clear views and cache. But nothing helps, it still uses default package views.
Views in /resources/views/vendor/elfinder exist.
Any idea how to make it work?
Try to edit the service provider file to be like that :
public function boot()
{
$this->loadViewsFrom($this->app->resourcePath('views/vendor/elfinder'), 'elfinder');
}
It is forces the file to use this directory as main view directory instead of changing it.
The question is quite old, but you have to set in the
$this->loadViewsFrom(__DIR__.'/../resources/views', 'courier');
the same package name as in the
$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/courier'),
]);
(notice both times the courier)
See: https://laravel.com/docs/8.x/packages#overriding-package-views
I have been going back and forth on this subject and could really use some insight.
I am writing an API in Laravel and an Web App in Laravel. I have been going back and forth in this process wondering if they should be in the same Laravel install or if they should be in separate installs. The pro's and con's I can think of for separate installs are as follows:
Pro's
api code doesn't interfere with web code
would use API for
data fetching and updating -> might be good for future scaling
Con's
Multiple laravel installations
Unable to use Eloquent (would save a
lot of work) -> since data would come from api.
What is the best way to do this?
Also, the real question is, if I were to keep them on the same installation, what is the best way to set up the folder structure to essentially run to apps on a single Laravel installation?
You can separate the code with namespaces. Use the composer.json file and PSR-4 so you can have separate code bases, even controller and models. Or you can use the same models in the Core namespace.
{
...
"autoload": {
"psr-4": {
"Api\\": "app/Api",
"Core\\": "app/Core",
"Web\\": "app/Web"
},
...
}
Surely you would want to keep the models together at least?
I recommend keeping the installs together and separating using routes:
Route::group( array('before'=>array('api'), 'prefix' => 'api' ), function() {
...
}
Route::group( array('before'=>array('auth'), 'prefix' => 'webapp' ), function() {
...
}
Structure is personal preference but I would have:
Controllers
|------API
| |-------MyController.php
|
|------WebApp
|-------MyController.php
And then in the controller use namespaces:
<?php
namespace API;
class MyController extends \APIBaseController {
Update:
If you want namespaces throughout then do this:
app
|
|------core
| |-------Models
|
|------api
| |-------Controllers
|
|------webapp
|-------Controllers
I am experiencing an issue trying to reference a particular Twig template. I am using the render method that is part of the SF2 main controller, but I clearly not referencing/using it correctly.
This is my directory/file structure:
/src
/AyrshireMinis
/CommonBundle
/Controller
DefaultController.php
/Entity
Link.php
/Resources
/views
/Default
links.html.twig
and this is the method called by the router in DefaultContoller.php:
/**
* #Route("/links", name="ayrshireminis_links")
* #Template()
*/
public function linksAction()
{
$links = $this->getDoctrine()->getRepository('AyrshireMinisCommonBundle:Link')->findAll();
return $this->render('AyrshireMinisCommonBundle:Link:links.html.twig', array('links' => $links));
}
But this is the error I get:
Unable to find template
"AyrshireMinisCommonBundle:Link:links.html.twig".
I think it's because the template "AyrshireMinisCommonBundle:Link:links.html.twig" does not exist, try changing it to "AyrshireMinisCommonBundle:Default:links.html.twig"
I had exactly the same problem with the same directory structure. The equivalent of 'AyrshireMinisCommonBundle:Default:links.html.twig' didn't work (meaning that the directory still could not be found). It worked when I changed it to 'Default/links.html.twig'
(Using Symfony 2.3 and PHP 5.3)
In my Laravel 4 application's root directory, I have a folder themes. Inside the themes folder, I have default and azure.
How can I access view from this themes/default folder in a specific route.
Route::get('{slug}', function($slug) {
// make view from themes/default here
});
My directory structure:
-app
--themes
---default
---azure
I need to load views from localhost/laravel/app/themes/default folder. Please explain this.
This is entirely possible with Laravel 4. What you're after is actually the view environment.
You can register namespace hints or just extra locations that the finder will cascade too. Take a look here
You'd add a location like so:
View::addLocation('/path/to/your/views');
It might be easier if you namespace them though, just in case you have conflicting file names as your path is appended to the array so it will only cascade so far until it finds an appropriate match. Namespaced views are loaded with the double colon syntax.
View::addNamespace('theme', '/path/to/themes/views');
return View::make('theme::view.name');
You can also give addNamespace an array of view paths instead of a single path.
Here I am not accessing my project from public folder. Instead of this I am accessing from project root itself.
I have seen a forum discussion about Using alternative path for views here. But I am little confused about this.The discussed solution was,
You'd add a location like,
View::addLocation('/path/to/your/views');
Then add namespace for theme,
View::addNamespace('theme', '/path/to/themes/views');
Then render it,
return View::make('theme::view.name');
What will be the value for /path/to/ ?
Can I use the same project in different operating system without changing the path?
Yes, we can do this using the following,
Put the following in app/start/global.php
View::addLocation(app('path').'/themes/default');
View::addNamespace('theme', app('path').'/themes/default');
Then call view like the default way,
return View::make('page');
This will render page.php or page.blade.php file from project_directory/app/themes/defualt folder.
I've developed a theme package for laravel 5 with features like:
Views & Asset seperation in theme folders
Theme inheritence: Extend any theme and create Theme hierarcies
Try it here: igaster/laravel-theme
\View::addLocation($directory); works fine but the new right way to do it is using loadViewsFrom($path, $namespace) (available on any service provider).