I am trying to implement CodeIgniter style folder structure to use HMVC in Laravel. I am following this tutorial. However I am unable to route to the controller inside of the modules folder. My current Laravel folder structure is:
App
modules
model
view
controller
UsersController.php
I want to route to some function of UsersController.
you can simply use route as you do with normal controllers inside of default controller folder
Route::any('name', array('name' => 'SomeController#someMethod'));
but name of two controller shouldn't be same i.e controllers on controller folder and controller inside your module folder must have different name. For same name you can use namespace.
Related
I am trying to create a simple action in my laravel nova app which exports a resource to pdf..
the controllers are in
app/http/controllers
when i try to use a controller inside my actions calling new blablaController.. it throws an error like
Class 'App\Nova\Actions\blablaController' not found
so, how do i say that my controller is in app/http/controllers ??
i have referenced my action in the controller like:
use App\Nova\Actions\BlaBla;
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
I am using the modular extensions HMVC add on for codeigniter.
my structure looks as follows:
modules/
-manager/
--controllers/
---manager.php
--views/
---index.php
the manager.php controller:
class Manager extends MX_Controller {
function __construct(){
parent::__construct();
}
function index(){
$data['newsletter'] = Newsletter::all();
$this->load->view('index',$data);
}
}
Routing and printing from inside the controller itself works fine, but I can't seem to load a view, get a codeigniter error saying the view file can't be found
/modules/manager/config/routes.php:
<?php
$route['module_name'] = 'manager';
It seems the views are still being called from CI's main view folder, not sure why they are not calling from the modules folder because the controller is extending the MX class
Try this:
$this->load->view('manager/index',$data);
Structure for folders:
apllication
modules
manager
config
routes.php
controllers
manager.php
views
index.php
Is there a way to alter the autorender views in cake 2.3.5, either but overriding something or method name conventions?
I want to render views in subfolders of the main Controller directory, here is an example:
class AdministrationController extends AppController {
public function products(){
$this->render('/Administration/products/index');
}
}
I would rather store the views in organized subfolders like this:
- Administration
- products
- index.ctp
- edit.ctp
My question is: Is there a way to rework this so that I don't have to use $this->render() in every action?
Yes
Either call render with a path relative to the corresponding view folder:
$this->render('products/index')
Or call with an "absolute" path which is understood to be relative to the view folder:
$this->render('/Administration/products/index');
Both of these calls will render the view file app/View/Administration/products/index. If you want to structure your view files like that - you either call render in each action or manipulate the viewPath variable to point where it needs to be (either in the class, or in the beforeFilter).
An Administration controller is not normal
In the question there is Administration , products and index - all normal things, but it's not normal to have an Administration controller. With that kind of controller structure the Administration controller will become huge.
The normal way to do that would be to use admin routing and define an admin index:
class ProductsController extends AppController {
function admin_index() {
//
}
}
I have a directory structure such that I have three directories inside my root directory, namely application, public and library.
Now, inside the library directory, I made a directory Custom, inside which I have a directory Controller, inside which I have a directory Action, inside which I have a directory Helper, and this directory contains a php file named 'LinkTo.php'. Inside this file, I have a class named Custom_Controller_Action_Helper_LinkTo which extends Zend_controller_Action_Helper and provides with a simple function called linkTo($inputString)..which outputs the url as per the input string parameter. But, I get this error "Action Helper by name CustomControllerActionHelperLinkTo not found " even though I have mentioned 'Custom_' in autoload namespaces in my application.ini, and have also taken care of include paths in my index.php.
Please help! How does one make an action helper like that and invoke it?
Did you specify path for the custom Action Helpers ?
You can do this in your application.ini, add following line:
resources.frontController.actionHelperPaths.Custom_Controller_Action_Helper_ = "Custom/Controller/Action/Helper"
After you specified path for your custom helpers, you need to initialize them for the later use. This can be done in Bootstrap:
protected function _initHelpers()
{
Zend_Controller_Action_HelperBroker::addHelper(new Custom_Controller_Action_Helper_LinkTo());
}
If you want to use helper as a method of the helper broker, for instance:
$this->_helper->LinkTo(); your custom helper should implement direct() method.