I'm building custom project and for now for database connection i used illuminate/database the eloquent model.
But i need samo pagination to continue my development i installed via composer illuminate/pagination and i cant configure it propertly i searched all over the internet no documentation for using with illuminate pagination or how to configure it.
I have function
User::all();
Where i fetch all my users successfully but i need to make paginate when i try to use ->paginate() method not found.
The composer is set coorectly and all my files are loaded.
Any tips how to set up the illuminate/pagination library ?
Btw i developed custom framework where i have controller and models. All my models are Eloquent models.
Thanks
You can check out the forPage() method on the Collection itself.
https://laravel.com/docs/5.8/collections#method-forpage
This worked for me:
$collection->forPage($_GET["pagenr"], $perpage);
use :
composer require illuminate/pagination
for example : if need Controllers and Models add autoload psr-4
composer.json
{
"name": "illuminate-example/eloquent",
"description": "Implementation of Database Queries with illuminate and Eloquent",
"type": "project",
"require": {
"illuminate/database": "^6.17",
"illuminate/pagination": "^6.17"},
"autoload":
{"psr-4":
{ "Controllers\\": "app/controllers/",
"Models\\": "app/models/"
}
}
}
Not sure if this is helpful anymore, but I was having the same issue and came across this: https://laracasts.com/index.php/discuss/channels/general-discussion/class-paginator-does-not-exist-laravel-42
Seems as though it's not possible outside laravel.
Related
This question already has an answer here:
Creating a custom helper in own package in laravel
(1 answer)
Closed 2 months ago.
Laravel 8, PHP 8.0
here is helper function autoload array in package composer.json
"autoload": {
"psr-4": {
"Chatify\\": "src/"
},
"files": [
"src/helper.php"
]
},
But When I try to call helper by any of its function inside blade file or in the controller its did not detect it. Its only detect the Application helper file.
I have trid these command composer dumpautoload and composer update, but it did not help.
I am trying to create a helper function for my custom package. I dont want to use Laravel Application Helper Function.
Its Already Solved here
Creating a custom helper in own package in laravel
Could not solve it by composer independently and used my package service provider. I added this code to the boot() method of my FaviconServiceProvider and it works now:
if (File::exists(__DIR__ . '\app\helpers.php')) {
require __DIR__ . '\app\helpers.php';
}
I am probably having some fundamental issue with understanding the Symfony Console component. I am trying to write a console-based application and I wanted to add the Doctrine bundle to it to create ORM-based entities, however, I am not able to load the bundle into my application.
What I found is that I should create app/AppKernel.php and add the bundle there:
public function registerBundles()
{
$bundles = array(
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
);
return $bundles;
}
I have then also added the AppKernel to the composer.json and dumped composer's autoload files:
"classmap": [ "app/AppKernel.php" ]
However, I am still unable to see any Doctrine commands in my console application's entry point. I don't even think the Kernel is loaded properly, the most confusing part to me is the difference between these two files on Github. First you have the Console component's Application.php which uses strings for its constructor parameters:
https://github.com/symfony/console/blob/master/Application.php
And then there's the same file in the Symfony Framework and that one uses the Kernel as a parameter:
https://github.com/symfony/framework-bundle/blob/master/Console/Application.php
So am I completely on the wrong path here? How do I load bundles / Kernels in a Console Component application? Or do I need the full Symfony package to do this sort of thing? That would seem kind of overkill if I just needed to write a somewhat complex terminal application.
Just in case it is any use, these are the relevant packages in my composer.json:
"symfony/console": "v3.3.*",
"symfony/yaml": "v3.3.*",
"doctrine/orm": "~2.5",
"doctrine/doctrine-bundle": "~1.6",
"doctrine/doctrine-cache-bundle": "~1.2",
If you create a console command which extends the ContainerAwareCommand as described here:
https://symfony.com/doc/current/console.html#getting-services-from-the-service-container
Then you can call:
$em = $this->container->get('doctrine.orm.entity_manager');
Which gives you the EntityManager allowing you to use Doctrine in your command line application. There is no need to edit your AppKernel or composer file to get Doctrine working.
EDIT with important note: The package I want to include does not use composer autoload. I'd have to use their hacky one and I want to avoid that.
I know how composer mostly works and I have package that can be a dependency (that's important, I know how to make this work in one project, but that's not what I'm asking).
What I need?
Somebody requires my package
composer require tomas/my-package
It will install
It will autoload my package with PSR-4
It will autoload 3rd party package as well
I have already tried something like this:
"autoload": {
"psr-4": {
"MyPackage\\": "src",
"PHP_CodeSniffer\\": "../../squizlabs/php_codesniffer/src"
}
}
I've tried that in one of my dependencies and it doesn't work :(.
Also, I've already talked to the package author and he doesn't want to use composer autoloading. He prefers his own.
Thanks for any help!
None if these answer are related to problem I desribed, so I try to post best solution so far:
I got inspired in PHPStan, which has feture to autoload directories, that were missed in composer (practically same issue).
I've added RobotLoader:
composer require nette/robot-loader
Then create LegacyCompatibility class with static method:
use Nette\Loaders\RobotLoader;
// ...
public static function autoloadCodeSniffer(): void
{
$robotLoader = new RobotLoader;
$robotLoader->acceptFiles = '*.php';
$robotLoader->setTempDirectory(sys_get_temp_dir() . '/_robot_loader');
$robotLoader->addDirectory(getcwd() . '/vendor/squizlabs/php_codesniffer/src');
$robotLoader->register();
}
And I call it, when needed:
LegacyCompatibility::autoloadCodeSniffer()
Works like a charm. But still opened to better solutions +1 !
Github Permalink
You are doing one big NO-NO in your composer.json file: You are defining autoloading for code that is not in your package.
This will fail, as you found out already.
And I wonder why you are struggling with integrating the PHP Codesniffer, because that package has a working autoloading definition included. All you'd need to do is "require": { "squizlabs/php_codesniffer": "^3.0#RC" } (if you really need the latest 3.x release candidate, otherwise ^2.8.0 would be a better idea).
If you require PHP_Codesniffer instead of somehow referencing it in your autoloading, then Composer will do everything to integrate that package: download it, add it to the autoloader, and run it's code if necessary. And anyone depending on YOUR package will also get this dependency installed.
The only problem you state is that "it doesn't work", which is no sufficient description of a problem because you lack describing what you did in your code, and what error message you got. Also state what you expected to happen, and what happened instead (and how this deviated from your expectation if this isn't obvious).
I have an ecommerce site that i am building for a client. This site had previously worked perfectly fine using procedural functions and curl to make the calls to Paypal. I download the Mailgun and Easypost API manual and installed manually. DOwn the road, I wanted to update the site to utilize PDO & OOP. I have done a fair job at getting the foundation laid. Now it is time to start making calls to the various API's.I installed everything using composer and currently run a dual autoloader. The composer autoload and then beneath it a custom autoload to load my classes. Now, when I call on the PayPal API, I get the following error:
Fatal error: Class 'Paypal\Rest\ApiContext' not found in /var/www/html/myla-dev/shoppingCartFinalize.php on line 18
I think what is happening is my autoloader is trying to load this rather than the composers autoloader. Here is where the autoloading is occurring:
init.php
require __DIR__.'/core/functions/general.php';
function autoLoader ($class) {
if (file_exists(__DIR__.'/core/classes/'.$class.'.php')) {
require __DIR__.'/core/classes/'.$class.'.php';
}
}
spl_autoload_register('autoLoader');
require __DIR__.'/vendor/autoload.php';
This file sits in project root directory and is then required at the top of every file.
File Structure
core
--classes
----Alert.php
----....
--functions
----general.php
vendor
--composer
--easypost
--guzzle
--mailgun
--symfony
--paypal
--autoload.php
index.php
init.php
...
composer.json
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/EasyPost/easypost-php"
}
],
"require": {
"php": ">=5.3.0",
"easypost/easypost-php": "dev-master",
"ext-curl": "*",
"ext-json": "*",
"paypal/rest-api-sdk-php":"*",
"mailgun/mailgun-php": "dev-master"
}
}
Any and all assistance is appreciated. If you feel like writing code, GREAT, but that is not what I am asking for. That is my job, but help with reworking to make it work would be awesome.
Thanks
It ended up being a simple syntax error. After I dumped and updated composer it still failed to work, so I just copied the code from the installation wiki. It worked so I composer their code to mine and I was missing a backslash in my call to the wiki. Thank you for your assistance though.
Is it possible to include a package that was not specifically designed for L4 in the framework?
If so, how is it done? I know I need to add the package to my composer.json which adds it to the vendor folder, but can I register it somehow in the providers array? are there any other steps necessary?
I would like to use the Google checkout package originally designed for Yii
Using third party composer packages with Laravel 4
When developers create composer packages, they should map the auto-loading using PSR-0 or PSR-4 standards. If this is not the case there can be issues loading the package in your Laravel application. The PSR-0 standard is:
{
"autoload": {
"psr-0": { "Acme": "src/" }
}
}
And the PSR-4 standard:
{
"autoload": {
"psr-4": { "Acme\\": "src/" }
}
}
Basically the above is a standard for telling composer where to look for name-spaced files. If you are not using your own namespaces you dont have to configure anything else.
SCENARIO 1
PSR-0 standard following packages (with autoload classmap) in Laravel
This is a simple one, and for example i will use the facebook php sdk, that can be found:
https://packagist.org/packages/facebook/php-sdk
Step 1:
Include the package in your composer.json file.
"require": {
"laravel/framework": "4.0.*",
"facebook/php-sdk": "dev-master"
}
Step 2:
run: composer update
Step 3:
Because the facebook package uses a class map its working out of the box, you can start using the package instantly. (The code example below comes straight from a normal view. Please keep your logic out from views in your production app.)
$facebook = new Facebook(array(
'appId' => 'secret',
'secret' => 'secret'
));
var_dump($facebook); // It works!
SCENARIO 2
For this example i will use a wrapper from the instagram php api. Here there need to be made some tweaks to get the package loaded. Lets give it a try!
The package can be found here:
https://packagist.org/packages/fishmarket/instaphp
Step 1:
Add to composer .json
"require": {
"laravel/framework": "4.0.*",
"fishmarket/instaphp": "dev-master"
}
Then you can update normally (composer update)
Next try to use the package like you did with the facebook package. Again, this is just code in a view.
$instagramconfig = array(
'client_id' => 'secret',
'client_secret'=> 'secret',
'access_token' => 'secret'
);
$api = Instaphp::Instance(null, $instagramconfig);
var_dump($api); // Epic fail!
If you try the above example you will get this error:
FatalErrorException: Error: Class 'Instaphp' not found in ...
So we need to fix this issue. To do this we can examine the instagram composer.json, that has its autoload diffrent than the facebook php sdk had.
"autoload": {
"psr-0": { "Instaphp": "." }
}
Compared to the facebook composer.json:
"autoload": {
"classmap": ["src"]
}
(Composer handles different kinds of autoloading, from files and class-maps to PSR. Take a look at your vendor/composer/ folder to see how its done.)
Now we will have to load the class, manually. Its easy, just add this (top of your controller, model or view):
use Instaphp\Instaphp;
composer dump-autoload, and it works!
step2 (optional)
Another method is (if you dont want to use the "use" statement, you can simply tell composer to look for the files straight from your code. Just change the Instance like so:
// reference the name-spaced class straight in the code
$api = Instaphp\Instaphp::Instance(null, $instagramconfig);
var_dump($api); // It works
However I suggest using the usestatement to make it clear to other developers (and your future self) what (external) classes/packages are used in the program.
SCENARIO 3
Here we use the Laravels built in IOC container to register service providers. Please note that some packages might not be suitable for this method. I will use the same Instagram package as in scenario 2.
Quick and dirty
If you don't care about design patterns and service providers you can bind a class like this:
App::bind('Instaphp', function($app)
{
return new Instaphp\Instaphp;
});
And you resolve it like this.
App::make('Instaphp');
Quick and dirty end
If you're working on a bigger project, and you make use of interfaces you should probably abstract the bindings further.
Step 1:
Create a folder inside your app folder, for example a 'providers' folder.
app/providers
Make sure Laravel auto-loads that folder, you can pass in some additional info to composer.json, like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/providers" // this was added
]
},
Now create a File inside the new folder called Instagram.php and place this inside:
<?php
use Illuminate\Support\ServiceProvider;
class InstagramServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('Instaphp', function()
{
return new Instaphp\Instaphp;
});
}
}
Now run composer dump-autoload again, and you can use the package. Note that the instagram package has a final private function __construct(), this means you cannot use that package outside the original class without changing the construct method to public. I'm not saying this is a good practice, and i suggest to use the scenario 2, in the case of the instagram package.
Anyway, after this you can use the package like this:
$instagramInstance = App::make('Instaphp');
$instagramconfig = array(
'client_id' => 'secret',
'client_secret'=> 'secret',
'access_token' => 'secret'
);
$instagram = new $instagramInstance();
$userfeed = $instagram->Users->feed($instagramconfig);
var_dump($userfeed); // It works!
Add "tvr/googlecheckout": "dev-master" this to your composer.json.
Run composer install, then you can use the IoC container. Some code examples can be found in the official docs for Laravel 4: http://four.laravel.com/docs/ioc#basic-usage