I am using CAKEPHP for creating my Application apis,
Currently I have few controllers in `
app/controllers/{UsersController, AdminController, StoresController}
` etc
I am accessing my controllers as //users/
However I want to add versioning system in cakephp
Something like
<ip>/<foldername>/v1/users/<action>
<ip>/<foldername>/v2/users/<action>
I tried creating a Folder inside Controllers/ as Controllers/v1/UsersController.php
However I am unable to access it.
How can i configure my routes
I'm quite confused about CakePHP version you are using (you tagged cakephp-3.0, and folder structure you used in question resembles that of 2.x), but both 2.x and 3.x versions have common concept called Prefix Routing, which is what you should use here. Below example assumes CakePHP 3.x:
You need to create directories (v1, v2) inside your Controller directory. Next, create individual controllers in that folder, remembering that namespace should also reflect this structure:
<?php
namespace App\Controller\v1;
use App\Controller\AppController;
class UsersController extends AppController {
//...
}
Final step is to configure routing in your routes.php. Example below:
Router::prefix("v1", function (RouteBuilder $routes){
$routes->fallbacks(DashedRoute::class);
});
More info can be found here:
Prefix Routing
For CakePHP 2.x things are little different:
In v2.x, you should NOT create folders inside Controller directory. All prefixed and unprefixed functions will reside in single controllers, eg.
class UsersController extends AppController {
/**
* Unprefixed function
*/
public function login(){
}
/**
* Prefix v1
*/
public function v1_login(){
}
/**
* Prefix v2
*/
public function v2_login(){
}
}
Next, you need to configure these prefixes in your app/Config/core.php file like so:
Configure::write('Routing.prefixes', array('v1', 'v2'));
After this, you should be able to access your actions with v1 and v2 prefixes, eg yourapp/v1/users/login will map to UsersController::v1_login() and yourapp/v2/users/login to UsersController::v2_login().
More info in documentation for 2.x: Prefix Routing v2.x
Related
I'm creating an application for a Symfony web project that I have made previously as a portable bundle. I've created a "media.html.twig" template and want it to be called by the render() helperfunction.
I've activated the bundle in /config/bundles.php with
App\OM\MediaManagerBundle\OMMediaManagerBundle::class => ['all' => true],
and followed the instructions on this site https://symfony.com/doc/current/bundles.html for setting up the Symfony-Bundle folder structure and the required files.
I've prefixed the path with "#MediaManager" as told by this site https://symfony.com/doc/4.1/bundles/best_practices.html#resources.
This is my Controller class:
<?php
namespace App\OM\MediaManagerBundle\Controller;
use App\OM\MediaManagerBundle\Form\ImageType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class MediaManagerController extends AbstractController
{
const TEMPLATE = "#MediaManager/MediaManager/media.html.twig";
/**
* #Route("/media/", name="om_mediamanager_media")
* */
public function index(Request $request)
{
$form = $this->createForm(ImageType::class);
return $this->render(self::TEMPLATE,
[
'form' => $form->createView(),
]);
}
}
This is how my folder structure looks like inside src:
https://i.imgur.com/9UomIEK.png
I hoped my form would be displayed but instead I got an error message saying:
There are no registered paths for namespace "MediaManager".
I know that it starts looking from the /templates folder so I tried setting the path to "../src/OM/MediaManagerBundle/Resources/views/MediaManager/media.html.twig" but got
Looks like you try to load a template outside configured directories (../src/OM/MediaManagerBundle/Resources/views/MediaManager/media.html.twig).
The code does work when I move the "media.html.twig" template inside the /templates folder but I expect it to ruin the portability of the bundle and therefor don't want to do that.
How do I access this template from inside my OMMediaManagerController class?
Am I even using Symfony Bundles the right way?
Is there any more documentation about Bundles in general? I found the ones provided by the official site to be very lacking.
I've had to try and work with an older package meant for php into my Laravel project.
I've added two custom classes, both are in the same folder "Classes" under the main "app" folder in my Laravel project.
One of these classes is recognized from a generated controller for my Laravel project. My paymentsController has use App\Classes\Quickbooks_Payments; in the top.
However, going to that Classes' file, I hit the following error through a route leading to my controller:
Class 'App\Classes\Quickbooks_Loader' not found
Now this is where this above is referenced in my paymentsController file:
<?php
namespace App\Classes\Quickbooks_Payments;
use App\Classes\Quickbooks_Loader;
/**
* QuickBooks Payments class
*
*/
/**
* Utilities class (for masking and some other misc things)
*/
QuickBooks_Loader::load('/QuickBooks/Utilities.php');
This last line is where the above error is referenced. And I do have both the Quickbooks_Loader.php and the Quickbooks_Payments.php in the same folder. My Quickbooks_Loader.php file starts off as such:
<?php
namespace App\Classes\QuickBooks_Loader;
So I know this is likely because of my inexperience with custom/imported classes. What am I doing wrong and how should I properly "import" these custom classes and have them recognized without any issues?
Change namespace in both classes you've shown to:
namespace App\Classes;
And run composer du
Also, make sure the class looks like this and not like you've shown (I'm not sure about did you cut something or not):
<?php
namespace App\Classes;
use App\Classes\Quickbooks_Loader;
class Quickbooks_Payments
{
public function __construct()
{
dd('It\'s working!');
}
}
So in my project I have one class that is placed in app folder (not inside app/http/controllers) and has middleware just App.
When I'm trying to get its method from routes.php it's not found because it's not in controllers folder and middleware.
What should I write instead of Route::get('/get', 'MyApi#get'); to make it all work?
First of all, for your API you should use something like Lumen, or, if you want it inside your main project, make nested folder inside your Controllers folder, and access it from routes like Api\MyApi#get.
If you really want it outside Controllers folder (absolutely no reason for it, IMHO), you need to change RouteServiceProvider namespace setting, to be empty string:
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = '';
// ...
}
Then specify full namespace for your controller and access it from wherever you want.
You can organize the controllers in subfolders if you namespace them correctly. I can't think of a valid reason to not put application controllers in the application its controllers directory.
See:
Route to controller in subfolder in Laravel 5
What is the best way to separate admin and front-end for a website in codeigniter where as I was to use all libraries, models, helpers etc. in common, but only controllers and Views will be separate.
I want a more proper way, up for performance, simplicity, and sharing models and libraries etc.
I highly suggest reading the methods outlined in this article by CI dev Phil Sturgeon:
http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter
My advice: Use modules for organizing your project.
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
Create a base controller for the front and/or backend. Something like this:
// core/MY_Controller.php
/**
* Base Controller
*
*/
class MY_Controller extends CI_Controller {
// or MX_Controller if you use HMVC, linked above
function __construct()
{
parent::__construct();
// Load shared resources here or in autoload.php
}
}
/**
* Back end Controller
*
*/
class Admin_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
// Check login, load back end dependencies
}
}
/**
* Default Front-end Controller
*
*/
class Public_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
// Load any front-end only dependencies
}
}
Back end controllers will extend Admin_Controller, and front end controllers will extend Public_Controller. The front end base controller is not really necessary, but there as an example, and can be useful. You can extend MY_Controller instead if you want.
Use URI routing where needed, and create separate controllers for your front end and back end. All helpers, classes, models etc. can be shared if both the front and back end controllers live in the same application.
I use a very simple approach: file folders. Check out the CI User Guide section, Organizing Your Controllers into Sub-folders.
I have my public-facing website built as any other would be built with CodeIgniter. Then I have two additional folders, controllers/admin and views/admin.
The admin controllers are accessed via http://[hostname]/admin/controller, and behave just as any other controller except they have specific authentication checks. Likewise, the views are simply called with the folder name included: $this->load->view('admin/theview');.
I haven't found a reason to do anything more complicated than that.
You all can find complete solution over here, https://github.com/bhuban/modular
Module separation for admin and front-end using HMVC and template separation using template libraries
I am using two third party libraries, you can find it in zip file.
HMVC for modular developed by wiredesignz
Template engine for templating by Phil Sturgeon
Just unzip it into your webserver root directory and run
localhost/modular for front-end
and
localhost/modular/admin for back-end
application/back-modules, it is for the back-end modules
application/front-modules, it is for the front-end modules
similarly
templates/admin for the back-end templates
templates/front for the front-end templates
themes/admin for the back-end themes
themes/front for the front-end themes
Nothing hacked in original code just configured using config.php and index.php
I don't know the terminology for my problem so I am asking here for the solution since I can't find it by searching.
I am making a simple rest API where I have specified my Routes.php
In my index.php I call the routes:
$routes = new Routes();
$routes->getRoutes();
app/Routes.php
<?php
/* All default application routes go here. Flow specific routes are implemented in the specific flows directory. */
class Routes extends Controller
{
function __construct()
{
/* Inherit stuff from Controller */
parent::__construct();
}
public function getRoutes()
{
/* Routes */
$this->app->group('/api', function () use ($app)
{
$this->app->get('/cron/run', 'Cron:run');
});
}
}
Now I want it to support standalone "modules".
So we add a module and its routes in here:
app/MyModule/Routes.php
Imagine multiple modules in here with their own routes.
Here is the problem.
How can I add routes in all these modules to be included automatically in the application routes?
I do not want to override.
I am using Slim PHP framework if that helps.
You simply need to instantiate each module's Routes class in turn within your bootstrapping process (i.e. before you call run()).
As long as each module defines a unique string for the group's URL (/api in your example), then they won't clash.