Laravel use non composer package - laravel

How can I use this non composer package to my controller?
https://github.com/lelag/otphp
I've tried
use OTPHP;
$totp = new \OTPHP\TOTP($secret);
and I get
Class 'OTPHP\TOTP' not found

You will need to require the file.
require_once('/path/to/otphp.php');
Or if you want to use composer.json, look into using classmaps

Related

Cannot locate class of installed package

I'm writing a laravel package which contains spatie/laravel-sitemap.
I already included several external packages and I didn't encountered any issues, but for some reason I'm not able to integrate this one.
What I did is the usual:
composer require spatie/laravel-sitemap
Then I have created a Console command that have as handle method the following content:
public function handle()
{
SitemapGenerator::create(config('app.url'))
->configureCrawler(function (Crawler $crawler) {
$crawler->ignoreRobots();
})
->writeToFile(public_path('sitemap.xml'));
$this->line('<info>Sitemap generated');
}
when I execute the command registered as:
php artisan myapp:sitemap
I get:
Class "Spatie\Sitemap\SitemapGenerator" not found
The reference imported are:
use Spatie\Crawler\Crawler;
use Spatie\Sitemap\SitemapGenerator;
I also tried composer update and composer dump-autoload, same problem.
Any help?
register package class in providers array in config/app.php
Spatie\Sitemap\SitemapServiceProvider;
in the bottom of app.php file
i hope it was useful.
you can publish package using this.
php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=sitemap-config
then
composer dump-autoload
for more details please check the document https://github.com/spatie/laravel-sitemap under Configuration

How to fix "Non-static method Spatie\Analytics\Analytics::fetchVisitorsAndPageViews() should not be called statically?"

When i put:
use Spatie\Analytics\Analytics;
It gives the error
'Non-static method should not be called statically'
But when I only put:
use Analytics;
I gives a white page on refresh or says
"The use statement with non-compound name 'Analytics' has no effect "
when starting.
I am using Laravel 5.5.4 and although it says the facade should be automatically setup, it wasn't working so I also added this manually to the // config/app.php:
'Analytics' => Spatie\Analytics\AnalyticsFacade::class,
But it still is not working.
from the package github. there was a solution
php artisan config:clear
but it did not work for me.
This package can be installed through Composer.
composer require spatie/laravel-analytics
In Laravel 5.5 and above the package will autoregister the service provider. In Laravel 5.4 you must install this service provider.
config/app.php
'providers' => [
...
Spatie\Analytics\AnalyticsServiceProvider::class,
...
];
In Laravel 5.5 and above the package will autoregister the facade. In Laravel 5.4 you must install the facade manually.
config/app.php
'aliases' => [
...
'Analytics' => Spatie\Analytics\AnalyticsFacade::class,
...
];
You want to use the facade to access the class, you will need to change:
use Spatie\Analytics\Analytics; to use Analytics;
Another way around just import this to your class:
use Spatie\Analytics\AnalyticsFacade as Analytics
It depends in what context you place the use statement.
In Laravel you also can use facades without having to import it with use.
The same class can be called by using \Analytics in your code call.
Example:
\Analytics::fetchMostVisitedPages(\Period::days(7));

Alias and provider name unknown for github library

I am trying to install this github library:
https://github.com/robholmes/term-extractor
I used composer by adding this in composer.json under "require":
"robholmes/term-extractor": "dev-master"
However, I'm not sure what I would put in the app.php config file under providers and aliases. What would be the lines I would add there?
This is not a Laravel specific package. No need to add service providers, as they don't exist.
composer require robholmes/term-extractor dev-master
Instead, just include it in your controller for use, etc:
use TermExtractor\TermExtractor;
To test it works (it should), in your controller method just return this:
$term_extractor = new TermExtractor();
return dd($term_extractor);

how to create new module in Laravel

Is there any simple way to create new module in Laravel, if yes then please explain in detail.
Need to create new custom module in Laravel for users, product etc but didnt understand how to proceed with the same.
These packages will not help you if you don't understanding what exactly are you doing. I really recommend you to start with this tutorial, for example.
You should be right to go with
php artisan module:make <module_name>
You can also create multiple modules at once doing this:
php artisan module:make Users Products
this command will create all the resources you need like controller, seed class service provider and scaffold all the routes. If you dont want all this create new module with the following command
php artisan module:make Products --plain
After research found solution on GITHub :
https://github.com/jaiwalker/setup-laravel5-package
This might be better way to create a new package.
If anybody have better solution, then please share.
To install through Composer, by run the following command:
composer require nwidart/laravel-modules
The package will automatically register a service provider and alias.
Optionally, publish the package's configuration file by running:
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
By default the module classes are not loaded automatically. You can autoload your modules using psr-4 (composer.json). For example :
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
}
}
Creating a module is simple and straightforward. Run the following command to create a module.
php artisan module:make <module-name>
It is also possible to create multiple modules in one command.
php artisan module:make Blog User Auth
You can refer this open source project to create module https://asgardcms.com/ - A modular multilingual CMS built with Laravel 5.

CodeIgniter + omnipay installation

I have used ci-merchant before but from everything see that the "V2" of it is now omnipay. I use codeigniter and i'm struggling to get even the example to work.
I have installed omnipay no problems and in my controller have the following:
use Omnipay\Common\GatewayFactory;
class Homepage extends BC_basecontroller {
public function index()
{
$gateway = GatewayFactory::create('PayPal_Express');
$gateway->setUsername('adrian');
$gateway->setPassword('12345');
}
}
Which is the example here: https://github.com/adrianmacneil/omnipay
However I get the error:
PHP Fatal error: Class 'Omnipay\Common\GatewayFactory' not found in......
Does anyone know how to get it to work in CI?
I'm not sure how you installed Omnipay, but you need to use Composer to load the classes before you can use them.
So following the Omnipay installation instructions, add this to a composer.json file in your root directory:
{
"require": {
"omnipay/omnipay": "*"
}
}
Then install the files:
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar update
Now, if you are using CodeIgniter you will need to set it up to include the composer autoloader. Basically, just add this line to the top of your index.php file:
require_once __DIR__.'/vendor/autoload.php';
There is also a tutorial on using Composer with CodeIgniter here which you may find helpful: http://philsturgeon.co.uk/blog/2012/05/composer-with-codeigniter
I had the same error and fixed it by loading vendor/autoload.php before application/core/CodeIgniter.php

Resources