Laravel folders for versioning - php

I am implementing versioning in Laravel. Now my doubt is, where should I create the V1 subfolders within the various main folders? For example, is it okay for controllers (Controllers/Api/V1) but what about Models, Resources, Request, Services etc...?

There is a very well maintained package specifically for what you are trying to do, called Laravel Modules: https://github.com/nWidart/laravel-modules
You can accomplish subversions of the application inside of it, and everything like "Models, Resource, Routes, etc.." is packaged seperately from one module to another.
Modules/
├── Blog/
├── Config/
├── Console/
├── Database/
├── factories/
├── Migrations/
├── Seeders/
├── Entities/
├── Http/
├── Controllers/
├── Middleware/
├── Requests/
├── Providers/
├── PostsServiceProvider.php
├── RouteServiceProvider.php
├── Resources/
├── assets/
├── lang/
├── views/
├── Routes/
├── api.php
├── web.php
├── Tests/
├── composer.json
├── module.json
├── package.json
├── webpack.mix.js
Check the documentation on how to use it: https://docs.laravelmodules.com/v9/creating-a-module
Also laravel-daily made a video on what it is here: https://www.youtube.com/watch?v=RVApkrYMcAg

Related

How to use Controller and Model in slim php framework with example

I have just got Slim PHP Framework with composer. Can anyone suggest me how to use Controller and Model in slim framework. And Where to put controllers and model in slim framework.Actually I want to use slim framework only for API. And I want to use controller for it. I have read its document but I am not able to find regarding controller and models.
Slim doesn't enforce any particular layout for your application.
I tend to like to separate out PHP source from HTML templates, so I use this pattern (taken from slim-bookshelf):
app/
├── src
│   ├── Bookshelf
│   │   ├── Author.php
│   │   ├── AuthorController.php
│   │   ├── Book.php
│   │   ├── BookController.php
│   │   └── TwigExtension.php
│   ├── dependencies.php
│   ├── middleware.php
│   └── routes.php
├── templates
│   ├── bookshelf
│   │   ├── author
│   │   │   ├── books.twig
│   │   │   ├── edit.twig
│   │   │   └── list.twig
│   │   └── book
│   │   └── list.twig
│   └── layout.twig
└── settings.php
However, my APIs do not have templates, so I move everything up a level. This example comes from slim-bookshelf-api and uses a separate class for each route's action rather than a controller class:
src
├── App
│   └── Action
│   ├── HomeAction.php
│   └── PingAction.php
├── Bookshelf
│   ├── Action
│   │   ├── CreateAuthorAction.php
│   │   ├── DeleteAuthorAction.php
│   │   ├── EditAuthorAction.php
│   │   ├── GetAuthorAction.php
│   │   └── ListAuthorsAction.php
│   ├── Author.php
│   ├── AuthorMapper.php
│   └── AuthorTransformer.php
├── dependencies.php
├── middleware.php
├── routes.php
└── settings.php

CodeIgniter not routing requests past /index.php/ to correct controller

New to CI, so it's probably safe to assume misunderstandings...
My folder structure looks like
.
└── www
├── appthing
│   └── V5
│   ├── application
│   │   ├── config
│   │   ├── controllers
│   │   │   ├── api
│   │   │   └── V1
│   │   │   ├── settings.php
│   │   ├── ...
│   │   └── views
│   └── system
└── vhosts
└── appthing
index.php
.htaccess
htaccess
My apache config in /etc/apache2/sites-available/000-default.conf looks like:
DocumentRoot /var/www/www/vhosts/appthing
<Directory "/var/www/www/vhosts/appthing">
AllowOverride All
</Directory>
When I navigate to ipaddress/index.php I get an authentication error, which is good, since I can at least access the index.php file. However, if I try to go to index.php/settings I get a CI 404 error. The same code works on my computer (OS X, trying to put it on a AWS Ubuntu 16 instance currently), so it's not a problem with CI semantics.
I think you need to set your Documentroot to /var/www/www/appthing/V5. Normally you should have and index.php file in your CI project which will redirect you to a home (standard = welcome.php) controller.

Create sub Module folders in laravel 5.1 using pingpong package

I am developing modular project in laravel 5.1 using pingpong package.Which gives me the project structure as below
laravel-app/
app/
bootstrap/
vendor/
modules/
├── Blog/
├── Assets/
├── Config/
├── Console/
├── Database/
├── Migrations/
├── Seeders/
├── Entities/
├── Http/
├── Controllers/
├── Middleware/
├── Requests/
├── routes.php
├── Providers/
├── BlogServiceProvider.php
├── Resources/
├── lang/
├── views/
├── Repositories/
├── Tests/
├── composer.json
├── module.json
├── start.php
I want to separate this modules folders in "admin" and "client" for differentiate my client and admin side like below,
laravel-app/
app/
bootstrap/
vendor/
modules/
├── Admin/
├── Blog/
├── Assets/
├── Config/
├── Console/
├── Database/
├── Migrations/
├── Seeders/
├── Entities/
├── Http/
├── Controllers/
├── Middleware/
├── Requests/
├── routes.php
├── Providers/
├── BlogServiceProvider.php
├── Resources/
├── lang/
├── views/
├── Repositories/
├── Tests/
├── composer.json
├── module.json
├── start.php
├── Client/
├── Blog/
├── Assets/
├── Config/
├── Console/
├── Database/
├── Migrations/
├── Seeders/
├── Entities/
├── Http/
├── Controllers/
├── Middleware/
├── Requests/
├── routes.php
├── Providers/
├── BlogServiceProvider.php
├── Resources/
├── lang/
├── views/
├── Repositories/
├── Tests/
├── composer.json
├── module.json
├── start.php
please help me out for this,
Thanks.
UPDATE:
You can mostly achieve what you are looking for by adjusting the config/modules.php file, but you will have to switch it back and forth when switching between Admin and Client.
For example:
To generate (module:make) or use (module:use) modules within the Admin portion of your project, you will need to do the following:
In the config/modules.php file, adjust the namespace to
/*
|--------------------------------------------------------------------------
| Module Namespace
|--------------------------------------------------------------------------
|
| Default module namespace.
|
*/
'namespace' => 'Modules\Admin',
In the same file, adjust the base_path to
/*
|--------------------------------------------------------------------------
| Modules path
|--------------------------------------------------------------------------
|
| This path used for save the generated module. This path also will added
| automatically to list of scanned folders.
|
*/
'modules' => base_path('modules/admin'),
That is all you need to do and calling php artisan module:make blog will create a Blog module within modules/admin.
If you need to switch between the Admin and the Client portion of your project, you will need to adjust the same two lines within the config/modules.php file to reflect as such.
There is one more caveat:
If you are planning to use the Assets folder within your modules, you will need to also adjust the corresponding line within the config/modules.php file, AND you will need to manually adjust a couple of methods with file paths explicitly written within your module's service provider (ex: Admin/Blog/Providers/BlogServiceProvider.php), AND you will need to fix your config/view.php - just follow the comments.
P.S. you can probably create a custom command to automate switching between Admin and Client.

ZF2: Zend DB, TableGateway, Services Strategies

I have created a little Zend Framework 2 module called CMS to write simple articles in my site. This module follow uses Zend Db and the TableGateway class in order to get the data from the database.
I have read many website and books about the strategies to create a module and I prefer the short and fast way using these file structure:
.
├── Module.php
├── config
│   └── module.config.php
├── data
│   └── data.sql
├── src
│   └── Cms
│   ├── Controller
│   │   ├── IndexController.php
│   │   ├── PageAdminController.php
│   │   └── PageCategoryAdminController.php
│   ├── Form
│   │   ├── Element
│   │   │   ├── PageCategories.php
│   │   │   └── ParentPages.php
│   │   ├── PageCategoryFilter.php
│   │   ├── PageCategoryForm.php
│   │   ├── PageFilter.php
│   │   └── PageForm.php
│   ├── Hydrator
│   │   └── Strategy
│   │   └── DateTimeStrategy.php
│   ├── Model
│   │   ├── Page.php
│   │   ├── PageCategory.php
│   │   ├── PageCategoryTable.php
│   │   ├── PageTable.php
│   │   └── UrlRewrites.php
│   └── View
│   └── Helper
│   ├── Extract.php
│   └── Tags.php
└── view
└── cms
├── index
│   ├── index.phtml
│   ├── notfound.phtml
│   └── page.phtml
├── page-admin
│   ├── edit.phtml
│   └── index.phtml
├── page-category-admin
│   ├── edit.phtml
│   └── index.phtml
└── partial
└── tags.phtml
TableGateway Method
This file structure allows me to declare, for instance, the Page and PageTable class in the module.php and call the ServiceLocator to read and write the records from the database in this way:
$pageTable = $this->getServiceLocator()->get('PageTable');
In this case I can use this class to write the CRUD methods in the PageTable Class.
Inject Service Method
Then I have seen that there is a Service way to do the same thing where the CRUD actions are located in a Service Class that calls the TableGateway Class and inject by a Factory Class the Service into the __construct method of the Controller.
Service > TableGateway > Factory > Controller
Why have I to choose the Service strategy instead of the simple TableGateway?
Well your logic is a little off, the way is actually only
Controller calls Service calls TableGateway
The Factory is just a pattern to properly inject the dependencies.
Why use the Service
To abstract the behavior. The Service generally speaking is the instrument for your controllers to get the data. The Service then interacts with a data-source. What the data-source is, your controller doesn't care - hell not even your service should care. The service should only care for an implementation of an interface. This way, whenever you feel that you don't like TableGateway anymore but you wanna go Doctrine2, you don't have to change your Service. You don't have to change you Controller. All you have to change is the dependency of your Service. Instead of injecting a TableGateway class you inject your Doctrine2 class which matches the interface of the data-provider-dependency.

Using a Zend partial view helper in Qunit tests

I'm quite new to both Zend and QUnit and I've got a bit stuck when it comes to setting up my QUnit tests to test my JavaScript.
I'm trying to test to some DOM manipulation and I understand that I have to put the html I'm testing against inside a #qunit-fixture div in my Qunit test file. However I've got a partial view helper used in my main Zend application (works fine in my view scripts) which I just want to echo out rather than having to rewrite the html used in the helper itself (to avoid duplication). The JavaScript I am attempting to test is used in the partial view helper hence wanting to use it.
Here's the folder structure for my application with just the important files and a few others remaining:
├── application
│   ├── Bootstrap.php
│   ├── configs
│   ├── controllers
│   ├── layouts
│   │   └── scripts
│   ├── models
│   ├── plugins
│   └── views
│   ├── helpers
│   └── scripts
│   ├── error
│   ├── index
│   └── partials
│   └── partialViewHelperIWantToUse.phtml
├── docs
├── features
├── Gemfile
├── Gemfile.lock
├── js-tests
│   ├── qunitTestFile.html
│   └── vendor
│   └── qunit
│   ├── qunit.css
│   └── qunit.js
├── library
│   ├── Custom
├── public
│   ├── css
│   ├── img
│   ├── index.php
│   └── js
│   ├── jquery
│   │   └── jquery-1.7.2.min.js
│   └── javaScriptIWantToTest.js
├── Rakefile
├── tasks
└── tests
├── application
│   ├── controllers
│   └── models
├── bootstrap.php
├── library
│   └── Custom
└── phpunit.xml
I want to do something like this in my qunitTestFile.html (found in js-tests)
<div id="qunit-fixture">
<?php echo $view->partial('partialViewHelperIWantToUse.phtml') ?>
</div>
but it currently doesn't work. Renaming the qunitTestFile to have a .phtml extension causes nothing to echo out but the error "call to a member function partial() on a non-object" appears in the apache error log (I expected that but it was a simple thing to try!). I have a hunch that I might need to create another bootstrap file or something but both and Zend and Qunit are new to me so I'm a bit lost. =)
I hope that's clear and I've given enough information but let me know if it isn't.
Thanks!
Since I do not exactly know which Zend version you use, I am not sure if this helps you:
The partial()-Function is a helper of your view. Since a View .phtml File is called in a Zend_View-Context (if you use an Controller-Action to show the View) you should use
$this->partial('...');
You can setup a Zend_View for yourself, but according your directory structure, I assume you use an MVC-Pattern.

Categories