My Directory set-up looks like this:
|- packages
|-|-- Iezon
|---|-- Core
|-- Admin
|-- src
|--|-- database
|-----|-- migrations
|--------|-- xxxx_xx_xx_xxxxxx_create_admin_panels.php
|--|-- Http
|----|-- routes.php
|--|-- Providers
|-----|-- AdminServiceProvider.php
|--|-- Resources
|-----|-- views
My AdminServiceProvider.php looks like this:
namespace Iezon\Core\Admin\Providers;
use Illuminate\Support\ServiceProvider;
class AdminServiceProvider extends ServiceProvider
{
public function boot()
{
$this->loadRoutesFrom(__DIR__ . '/../Http/routes.php');
$this->loadViewsFrom (__DIR__ . '/../Resources/views', 'admin');
}
public function register()
{
$this->publishes([__DIR__ . '/../database/migrations', database_path('migrations')]);
}
protected function loadRoutesFrom(string $path)
{
require_once $path;
}
}
My composer.json looks like this:
"autoload": {
"psr-4": {
...
"Iezon\\Core\\Admin\\": "packages/Iezon/Core/Admin/src",
...
}
}
When I run composer dumpautoload and then try to run:
php artisan vendor:publish --provider="Iezon\Core\Admin\Providers\AdminServiceProvider"
I get this:
Unable to locate publishable resources.
I then check it exists with php artisan vendor:publish and it does not show in the list of Providers. What am I missing?
$this->publishes(...) actually belongs inside the boot method, not in register. Also, you need to pass an associative array, but instead you pass two individual values. Finally, it is a best practice to only call publishes when the provider is actually running in console like this:
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__ . '/../database/migrations' => database_path('migrations')]);
}
Related
I've been trying to resolve this issue for two days now. I'd really appreciate some help.
We are going to use a single codebase for multiple domains. I can not use any multitenancy packages available because our codebase is currently a hybird one which is mixed with codeigniter.
So I wrote a package that creates env files under the app root as .envs/.env.{$id} ... so they are all in the .envs folder. My package creates the env files etc and has everything I need on the created env file.
I wrote a middleware for the same package to make laravel load the created env file. this is where it does not work. My package's service provder
namespace ODS\Tenant;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\ServiceProvider;
use ODS\Tenant\Console\CreateEnvFileCommand;
use ODS\Tenant\Http\Middleware\LoadTenantConfig;
class TenantServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('tenantmanager', function ($app) {
return new TenantManager();
});
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'tenant');
}
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
CreateEnvFileCommand::class,
]);
}
$kernel = app()->make(Kernel::class);
$kernel->pushMiddleware(LoadTenantConfig::class);
}
}
and the middleware for the package
<?php
namespace ODS\Tenant\Http\Middleware;
use Closure;
use Dotenv\Dotenv;
use ODS\Tenant\TenantManager;
use ODS\Tenant\EnvFileManager;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Artisan;
class LoadTenantConfig
{
public function handle($request, Closure $next)
{
$did = TenantManager::findDistrictId();
$envFile = App::basepath()."/.envs/.env.{$did}";
if (! is_file($envFile)) {
$envFileManager = new EnvFileManager($did);
$envFileManager->makeEnvFile($did);
}
$dotenv = Dotenv::createMutable(App::basePath().'/.envs', ".env.{$did}");
$dotenv->load();
Artisan::call('config:clear');
Artisan::call('cache:clear');
App::useEnvironmentPath(App::basePath() . '/.envs');
App::loadEnvironmentFrom(App::basePath(). "/.envs/.env.{$did}");
// App::configPath($envFile);
// dd(App::environmentFile());
// dd(config('app'));
return $next($request);
}
}
What is interesting is that in the middleware above, couple of lines before the end of file, I have dd(App::environmentFile()); and it actually returns the new env file under the .envs folder.
but
in a random controller when I do something like echo config('app.name');, it is still the value from the default .env file. I do not understand what is happening. When I do dd($_ENV); in my controller, I see the values so the Dotenv part is working. I have in my config files to use the env or default to value but it is using the env values from the default .env file instead of the .envs/.env.{$id} file
I'm trying to build a site with slim and autoloading my controller classes for the routes. I'm currently setting up the base structure and testing with a single route with nothing more than a simple "Test" output.
I prevously did this stuff with defining a spl_autoload_register function, but as this approach isn't recommended by slim and composer I want to do it right and I'm not trying to autoload my classes.
My Project is set up as this:
The class BlockController inside the file with the same Name under Controller is inside a namespace defined with namespace MyAPI\Controller;
app/Controller/BlockController.php
namespace MyAPI\Controller;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class BlockController
{
public function getList(Request $request, Response $response, $args)
{
return $response->withStatus(200)
->withHeader('Content-Type', 'text/html')
->write("Test");
}
}
I'm loading the dependencies and settings and after that all my routes (which contains currently only some small ones for testing my architecture):
public/index.php:
require __DIR__ . '/../vendor/autoload.php';
$settings = require __DIR__ . '/../app/settings.php';
$app = new \Slim\App($settings);
require __DIR__ . '/../app/dependencies.php';
require __DIR__ . '/../app/routes.php';
$app->run();
app/routes.php (is very simple, will be extended with more Route-files):
require 'Routes/BlockRoute.php';
app/Routes/BlockRoute.php:
use MyAPI\Controller\BlockController;
$container["BlockController"] = function ($container) {
return new BlockController($container);
};
$app->group('/block', function() use ($container) {
$this->get('[/]', 'BlockController::getList');
});
So the first command inside BlockRoute.php is the use of the BlockController-namespace. Everything under app/ should have the Base-Namespace MyAPI.
As described in the slim-documentation I planned to do that with to autoload feature of composer, so I modified my composer.json and added the following:
{
"require": { .. },
"autoload": {
"psr-4": {
"MyAPI\\": "app"
}
}
}
Edit: updated path to app-folder after the answer from Adam Lavin
After that I ran composer update. Is that the right command for those changes? Or should I use composer install? Couldn't find any more information what I have to do after making those additions in the autoload-section.
When I run the site now with the php webserver and navigate to this route /block I get the following RuntimeException:
Callable BlockController::getList does not exist
File: C:\Prog\src\vendor\slim\slim\Slim\CallableResolver.php
So the problem is that BlockController doesn't get included/autoloaded correctly but I don't understand why or what exactly the problem is. I tried to find some examples of working configurations with slim+composer+autoloading of classes but couldn't find something related.
Any input appreciated.
Since you're pointing MyApp\\ to ../src (the same directory as composer), the autoloader is going to try and find the controller in src/Controllers/BlockController.php.
It should be pointing to ../src/app, though since composer.json is in the src folder it can be simplified to app in the resulting composer.json file.
{
"require": { .. },
"autoload": {
"psr-4": {
"MyAPI\\": "app"
}
}
}
Additionally, In your example, the namespace of the BlockController is MoinAPI\Controllers, and should be MyAPI\Controllers.
And finally, in slim, you use a single colon instead of a double to refer to a callable route. BlockController::getList should be BlockController:getList
Run this from within docker container, or using the same php binary that composer used.
composer dump-autoload -o -vvv #-o fixed my problem in my case
This question already has answers here:
How to create custom helper functions in Laravel
(23 answers)
Closed 7 months ago.
If I wanted to make a currentUser() function for some oauth stuff I am doing where I can use it in a view or in a controller (think rails, where you do helper_method: current_user in the application controller).
Everything I read states to create a helpers folder and add the function there and then that way you can do Helpers::functionName Is this the right way to do this?
Whats the "laravel way" of creating helper functions that can be used in blade templates and controllers?
Create a new file in your app/Helpers directory name it AnythingHelper.php
An example of my helper is :
<?php
function getDomesticCities()
{
$result = \App\Package::where('type', '=', 'domestic')
->groupBy('from_city')
->get(['from_city']);
return $result;
}
generate a service provider for your helper by following command
php artisan make:provider HelperServiceProvider
in the register function of your newly generated HelperServiceProvider.php add following code
require_once app_path('Helpers/AnythingHelper.php');
now in your config/app.php load this service provider and you are done
'App\Providers\HelperServiceProvider',
An easy and efficient way of creating a global functions file is to autoload it directly from Composer. The autoload section of composer accepts a files array that is automatically loaded.
Create a functions.php file wherever you like. In this example, we are going to create in inside app/Helpers.
Add your functions, but do not add a class or namespace.
<?php
function global_function_example($str)
{
return 'A Global Function with '. $str;
}
In composer.json inside the autoload section add the following line:
"files": ["app/Helpers/functions.php"]
Example for Laravel 5:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": ["app/Helpers/functions.php"] // <-- Add this line
},
Run composer dump-autoload
Done! You may now access global_function_example('hello world') form any part of your application including Blade views.
Laravel global helpers
Often you will find your self in need of a utility function that is access globally throughout you entire application. Borrowing from how laravel writes their default helpers you're able to extend the ability with your custom functions.
Create the helper file, not class
I prefer to you a file and not a class since I dont want to bother with namespaces and I want its functions to be accessible without the class prefixes like: greeting('Brian'); instead of Helper::greeting('Brian'); just like Laravel does with their helpers.
File: app/Support/helper.php
Register helper file with Composer: composer.json
{
...
"autoload": {
"classmap": [
"database"
],
"files": [
"app/Support/helpers.php"
],
"psr-4": {
"App\\": "app/"
}
},
...
}
Create your first helper function
<?php
if (!function_exists('greet')) {
/**
* Greeting a person
*
* #param string $person Name
* #return string
*/
function greet($person)
{
return 'Hello ' . $person;
}
}
Usage:
Remember to autoload the file before trying to access its functions:
composer dump-autoload
Let's test with Tinker
$ php artisan tinker
Psy Shell v0.8.17 (PHP 7.0.6 ΓÇö cli) by Justin Hileman
>>> greet('Brian');
=> "Hello Brian"
>>> exit
Exit: Goodbye.
With Blade
<p>{{ greet('Brian') }}</p>
Advanced usage as Blade directive:
A times you will find yourself wanting to use a blade directive instead of a plain function.
Register you Blade directive in the boot method of AppServiceProvider: app/Providers/AppServiceProvider.php
public function boot()
{
// ...
Blade::directive('greet', function ($expression) {
return "<?php echo greet({$expression}); ?>";
});
}
Usage:
<p>#greet('Brian')</p>
Note: you might need to clear cache views
php artisan view:clear
The above answers are great with a slight complication, therefore this answer exists.
utils.php
if (!function_exists('printHello')) {
function printHello()
{
return "Hello world!";
}
}
in app/Providers/AppServiceProvider.php add the following in register method
public function register()
{
require_once __DIR__ . "/path/to/utils.php"
}
now printHello function is accessible anywhere in code-base just as any other laravel global functions.
Another option, if you don't want to register all your helper functions one by one and wondering how to register them each time you create a new helper function:
Again in the app/Providers/AppServiceProvider.php add the following in register method
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename) {
require_once($filename);
}
}
I would like to divide my application in modules. For instance, there would be a "core" modules that contains the basic login functionality, app layout/formatting (CSS etc), user management and a diary.
Later on I may create other modules like a contact manager that can easily be added or removed from the application.
There would be some logic in the apps navigation for determining which modules are present and to show/hide the links to them.
How can I do this in terms of directory structure, namespaces and anything else that's needed?
I am looking at creolab/laravel-modules but it states that it is for Laravel 4. Can I still use it with 5 in exactly the same way?
The documentation says to place models, controllers and views within each module directory, but how does this work with routes? Ideally I would like each module to have its own routes.php file. How will all of this work with the stuff in the http and the resources directory?
I was thinking of something like this:
But I have no idea how I would get it to work.
I have just tried the tutorial here:
http://creolab.hr/2013/05/modules-in-laravel-4/
With no extra libraries etc, just pure Laravel 5.
I seem to have hit a brick wall with an error message:
FatalErrorException in ServiceProvider.php line 16:
Call to undefined method Illuminate\Config\Repository::package()
Regarding the following:
<?php namespace App\Modules;
abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
if ($module = $this->getModule(func_get_args())) {
$this->package('app/' . $module, $module, app_path() . '/modules/' . $module);
}
}
public function register()
{
if ($module = $this->getModule(func_get_args())) {
$this->app['config']->package('app/' . $module, app_path() . '/modules/' . $module . '/config');
// Add routes
$routes = app_path() . '/modules/' . $module . '/routes.php';
if (file_exists($routes)) require $routes;
}
}
public function getModule($args)
{
$module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null;
return $module;
}
}
What is causing this and how can I fix it?
Got my head around this a bit more now. Got my package/module routes and views working which is great:
abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
if ($module = $this->getModule(func_get_args())) {
include __DIR__.'/'.$module.'/routes.php';
}
$this->loadViewsFrom(__DIR__.'/'.$module.'/Views', 'core');
}
public function register()
{
if ($module = $this->getModule(func_get_args())) {
}
}
public function getModule($args)
{
$module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null;
return $module;
}
}
I have one last question, how would I load all my controllers from inside my package, much like how the loadViewsFrom() method works?
I seem to have figured it all out.
I'll post it here in case it helps other beginners, it was just about getting the namespaces right.
In my composer.json I have:
...
"autoload": {
"classmap": [
"database",
"app/Modules"
],
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
}
My directory and files ended up like this:
I got my Core module router.php to work by wrapping my controllers for that module in a group specifying the namespace:
Route::group(array('namespace' => 'Modules\Core'), function() {
Route::get('/test', ['uses' => 'TestController#index']);
});
I imagine when I come to doing my models for the package it will be a similar case of getting the namespaces right.
Thanks for all your help and patience!
Solution:
Step1: Create Folder “Modules” inside “app/”
Step2: In Modules folder create your Module (Module1( suppose admin Module))
Inside admin module : create the following folder
1. Controllers (here will your controller files)
2. Views (here will your View files)
3. Models (here will your Model files)
4. routes.php (here will your route code in this file)
Similarly, you can create multiple modules
Module2( suppose API )
-Controllers
-Views
-Models
-routes.php
Step3 : Create ModulesServiceProvider.php inside “Modules/” Folder
Step4 : Paste following code inside ModulesServiceProvider.php
<?php
namespace App\Modules;
/**
* ServiceProvider
*
* The service provider for the modules. After being registered
* it will make sure that each of the modules are properly loaded
* i.e. with their routes, views etc.
*
* #author kundan Roy <query#programmerlab.com>
* #package App\Modules
*/
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class ModulesServiceProvider extends ServiceProvider {
/**
* Will make sure that the required modules have been fully loaded
*
* #return void routeModule
*/
public function boot() {
// For each of the registered modules, include their routes and Views
$modules=config("module.modules");
while (list(,$module)=each($modules)) {
// Load the routes for each of the modules
if (file_exists(DIR.'/'.$module.'/routes.php')) {
include DIR.'/'.$module.'/routes.php';
}
if (is_dir(DIR.'/'.$module.'/Views')) {
$this->loadViewsFrom(DIR.'/'.$module.'/Views',$module);
}
}
}
public function register() { }
}
Step5 : Add following line inside ‘config/app.php’ file
App\Modules\ModulesServiceProvider::class,
Step6 : Create module.php file inside ‘config’ folder
Step7 : Add following code inside module.php (path =>
“config/module.php”)
<?php
return [
'modules'=>[
'admin',
'web',
'api'
]
];
Note : You can add your module name whichever you have created. Here there are modules.
Step8 : Run this command
composer dump-autoload
A little late, but if you want to use modules in your future projects, i've written a module generator. It generates modules via php artisan make:module name You can also just drop some modules in the app/Modules folder and they are ready to use/work.
Take a look. Save some time ;)
l5-modular
You can also use pingpong-labs
documentations Here.
Here is an example.
You can just install and check the process.
Note: I am not advertising. Just checked that cms built on Laravel with module support. So thought that might be helpful for you and others.
Kundan roy: I liked your solution but I copied your code from StackOverflow, I had to change the quotes and semi-quotes to get it working - I think SOF replace these. Also changed Dir for base_path() to be more inline with Laravel's (new) format.
namespace App\Modules;
/**
* ServiceProvider
*
* The service provider for the modules. After being registered
* it will make sure that each of the modules are properly loaded
* i.e. with their routes, views etc.
*
* #author kundan Roy <query#programmerlab.com>
* #package App\Modules
*/
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class ModulesServiceProvider extends ServiceProvider
{
/**
* Will make sure that the required modules have been fully loaded
* #return void routeModule
*/
public function boot()
{
// For each of the registered modules, include their routes and Views
$modules = config("module.modules");
while (list(,$module) = each($modules)) {
// Load the routes for each of the modules
if(file_exists(base_path('app/Modules/'.$module.'/routes.php'))) {
include base_path('app/Modules/'.$module.'/routes.php');
}
// Load the views
if(is_dir(base_path('app/Modules/'.$module.'/Views'))) {
$this->loadViewsFrom(base_path('app/Modules/'.$module.'/Views'), $module);
}
}
}
public function register() {}
}
pingpong/modules is a laravel package which created to manage your large laravel app using modules. Module is like a laravel package for easy structure, it have some views, controllers or models.
It's working in both Laravel 4 and Laravel 5.
To install through composer, simply put the following in your composer.json file:
{
"require": {
"pingpong/modules": "~2.1"
}
}
And then run composer install to fetch the package.
To create a new module you can simply run :
php artisan module:make <module-name>
- Required. The name of module will be created.
Create a new module
php artisan module:make Blog
Create multiple modules
php artisan module:make Blog User Auth
for more visit: https://github.com/pingpong-labs/modules
I have made the below composer view for my app. I've placed it in separate file at app/composers.php.
<?php
// namespace App\Modules\Manager\Composer;
// use Illuminate\Support\Facades\View as View ;
/*
|--------------------------------------------------------------------------
| Composers
|--------------------------------------------------------------------------
|
|
*/
View::composer('tshop.includes.header', function($view)
{
$categories = Categories::getWithChilds();
$view->withCategories( $categories);
});
My composer.php file is
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"files": [
"app/composers.php"
]
},
Unfortunately I get this error
Fatal error: Class 'View' not found in C:\xampp\htdocs\eshop\app\composers.php on line 15
Update
I also tried this. I wrote inside app/start/global.php
require app_path().'/composers.php';
and
use Illuminate\Support\Facades\View as View ;
at app/composers.php, getting this error
Fatal error: Call to a member function composer() on a non-object in
C:\xampp\htdocs\eshop\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php
on line 211
I don't think your app/composers.php should be autoloaded within composer. Composer's responsibility is to resolve packages and install them for you, which has nothing to do with your application logic, let alone your application's views.
At the point of running composer, it would not have any knowledge of your Laravel app. That means your Laravel facades like View, Input, DB, Auth, etc. are not loaded yet. Thus your code throws Call to a member function composer() on a non-object.
Approach 1:
Laravel does not strictly specify where you put your laravel view composers, so requiring it by adding:
require app_path() . '/composers.php';
at the bottom of app/start/global.php like edi9999 said would be fine.
Don't forget to remove in this case:
"files": [
"app/composers.php"
]
Approach 2: there is a way to autoload your view composers in composer.json!
From example in Laravel docs on view composers, you can do something like...
app/viewcomposers/HeaderViewComposer.php:
class HeaderViewComposer
{
public function compose($view)
{
$categories = Categories::getWithChilds();
$view->withCategories( $categories);
}
}
composer.json:
"classmap": [
...
"app/viewcomposers"
]
app/composers.php:
View::composer('tshop.includes.header', 'HeaderViewComposer');
bottom of app/start/global.php:
require app_path() . '/composers.php';
Unfortunately you still need to add the line above to app/start/global.php so Laravel knows what view composers are defined.
Approach 3: Do autoload class in composer.json + register a custom ServiceProvider
Learning from Using View Composers in Laravel 4 by Philip Brown, we could also add our own custom service provider and not having to edit our app/start/global.php file.
app/viewcomposers/HeaderViewComposer.php:
<?php namespace App\Modules\Manager\Composer;
class HeaderViewComposer
{
public function compose($view)
{
$categories = Categories::getWithChilds();
$view->withCategories( $categories);
}
}
composer.json:
"classmap": [
...
"app/viewcomposers"
]
app/viewcomposers/ViewComposerServiceProvider.php:
<?php namespace App\Modules\Manager\Composer;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
public function register()
{
$this->app->view->composer('tshop.includes.header', 'App\Modules\Manager\Composer\HeaderViewComposer');
}
}
app/config/app.php:
'providers' => array(
...
'App\Modules\Manager\Composer\ViewComposerServiceProvider',
),
As #TheShiftExchange found out, one problem is that you used the "files" options.
As you can see in composer's code, the autoload section corresponds to this:
class ComposerAutoloaderInitf8489489s7f894ds98f47d
{
....
....
public static function getLoader()
{
....
....
$includeFiles = require __DIR__ . '/autoload_files.php';
foreach ($includeFiles as $file) {
composerRequiref4s65f4556sd4f564fsdfd($file);
}
return $loader;
}
}
function composerRequire5894s89f4sd98498489f7b37d($file)
{
require $file;
}
So the files array you specify is required during composer's autoload process, way before the View Facade is loaded.
The providers to the facades are loaded in vendor/laravel/framework/illuminate/foundation/start.php
/*
|--------------------------------------------------------------------------
| Register The Core Service Providers
|--------------------------------------------------------------------------
|
| The Illuminate core service providers register all of the core pieces
| of the Illuminate framework including session, caching, encryption
| and more. It's simply a convenient wrapper for the registration.
|
*/
$providers = $config['providers'];
$app->getProviderRepository()->load($app, $providers);
Actually, the problem with classmaps is an other one: It is that they is no class in your file, so the file will never be loaded so it doesn't do anything.
To make it work, you should add into app/start/global.php at the end of the file:
Instead of
require app_path() . '/filters.php';
Write
require app_path() . '/composers.php';
require app_path() . '/filters.php';
That's the best way I can think of to include files at each load of your application that are not classes.