I have opened up a question on CodePlex but have not received any responses at all.
Basically, I have a base controller that all of my controllers inherit from. When I create a new MVC3 project, put my controllers in another assembly, and add MEFContrib.MVC3 to the project, everything works great. When I make any of the controllers inherit from my base class, they can no longer be found.
I am not familiar enough with MEFContrib to know what exactly is breaking, but I have tried to decorate my controllers with ExportAttributes and that has not worked, either.
All of this is a different assembly than the main MVC project:
public class MyBaseController : Controller
{
...
}
// This controller cannot be found.
public class HomeController : MyBaseController
{
public ActionResult Index ()
{
// Do Stuff
return View ();
}
}
// This controller can be found.
public class HomeController : Controller
{
// yada, yada, yada...
}
Edit:
counsellorben was exactly right. I had dependencies that were not properly marked for export and so the controllers could not be properly composed. I stepped through each of my dependencies, stepping through their own dependencies, and found the few I missed.
The most likely issue is that there is a problem with a dependency in your MyController class. This will cause a problem with any controller inheriting from MyController.
Please see this answer for some sample code you can use to try and diagnose where your problem lies.
I think you need to decorate with the InheritedExport attribute.
Related
I need help with using sub directory controllers in CodeIgniter 4.
I just can't make it work for some reason.
This is the URL for example: www.example.com/admin/dashboard
In the controllers folder, I created a folder named Admin, and a file named Dashboard.php.
I used this code in Dashboard.php:
namespace App\Controllers;
class Dashboard extends BaseController
{
public function index()
{
}
}
I tried changing the class name to AdminDashboard, Admin_Dashboard, pretty much every logical name but every time I get a 404 error, saying:
Controller or its method is not found:
App\Controllers\Admin\Dashboard::index
I know the file itself gets loaded successfully, but I think I don't declare the classname correctly and it keeps throwing me those 404 errors.
The documentation of CI4 isn't providing any information about what the classname should be called unfortunately...
UPDATE #1
I managed to make it work by changing few things:
namespace App\Controllers\Admin;
use CodeIgniter\Controller;
class Dashboard extends Controller
{
public function index()
{
}
}
But now it won't extend the BaseController which has some core functions that I built for my app.
Any ideas to how to make it extend BaseController?
I must admit that I don't have much knowledge about namespacing yet, so that might be the reason for my mistakes.
As I imagined, the problem was that I didn't learn about namespacing.
I needed to point the use line at the BaseController location.
namespace App\Controllers\Admin;
use App\Controllers\BaseController;
class Dashboard extends BaseController
{
public function index()
{
}
}
Now www.example.com/admin/dashboard/ goes directly to that index function, as intended.
php spark make:controller /Subfolder/ControllerName
$routes->add('/(.+?)_(.+?)/(.+?)$', 'subdir\\\\$1_$2::$3');
$routes->add('/(.+?)_(.+?)$', 'subdir\\\\$1_$2::index');
I was able to map with this setting.
The route mapping could be as simple as:
$routes->group('admin', static function ($routes) {
$routes->get('dashboard', 'Admin\Dashboard::index');
});
I would like to have a method available to all Views in my app.
I would like to be able to make calls like this:
<span>${ getDynamicText() }</span>
The most obvious ways (to me) to implement this are:
Call the method in the controller and and pass it to the View.
Make the method static on some Util class and call it from the code ${ UtilClass.getDynamicText() }
Using meta programming to somehow make the method available to all Views.
The benefit of #3 is that the change would only have to be made in one place. #1 would have to be made in each controller action; and #2 would need an import on every View page which wants to use the method.
So is there a way to add a method to be available to all views in my app?
I have to admit I don't know in a lot of detail how .gsp files are processed behind-the-scenes so maybe they don't have a corresponding class and therefore can't be manipulated in this way. Links to good articles/docs will get extra good karma.
GSPs are compiled into classes that extend org.codehaus.groovy.grails.web.pages.GroovyPage, so you can add methods to that metaclass and they'll be available to all GSPs. The best place to do this is in BootStrap.groovy (or in a plugin's doWithDynamicMethods closure):
import org.codehaus.groovy.grails.web.pages.GroovyPage
class BootStrap {
def init = { servletContext ->
GroovyPage.metaClass.getDynamicText = { ... }
}
}
The recommended way to reuse functionality across GSPs is to define it as a tag, e.g.
class MyTagLib {
static namespace = 'my'
def dynamicText = {attrs ->
out << 'not very dynamic'
}
}
You can then call this tag in a GSP using:
<my:dynamicText/>
4th way: make a class/service that have method '.getDynamicText' and put it's intance into request at before filter ( request.setAttribute('x', myDynamicTextGeneratorObject) )
Now you can use x.dynamicText in any GSP
This is how I would do it:
Add a new class to your controllers folder containing your method
Do a grails install-templates
Navigate to the templates: \src\templatesscaffolding
Add the extends part to the controller template: class ${className}Controller extends NewController
re-generate your controllers
You can now use the method in every class and gsp.
I've never worked with Zend Framework before, but I've worked with others (CodeIgniter, Kohana, etc). Right now I was asked to just show a view that wasn't existing so I started looking into the documentation and examples I could find and I've always found examples that use the Model part of the MVC, but in this case I just need to load a view and I can't figure out how to do that. I have this:
File: "BookController":
require_once("Initiate.php");
class BookController extends Initiate {
public function init() {
parent::init();
}
public function bookAction(){
#$client = Zend_Auth::getInstance()->getIdentity();
$view = new Zend_View();
echo $this->view->render('book.phtml');
#$this->view->assign("book", $client);
#echo $this->view->render('book.phtml');
}
the view is called "book.phtml" and is found in /application/views/scripts/bookapi/
What am I missing?
Given that eveything is setup correctly with MVC, your Controller should extend Zend_Controller_Action:
class BookController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->funnyText = 'This is a funny text.';
}
}
Then, in your application/views/scripts/book/ folder, there has to be a index.phtml. Which could look like that:
<p>
<?php echo $this->funnyText; ?>
</p>
That's it, nothing more required.
Btw. doesn't make sense to have a controller called book and then also an action called book
you don't have to manage view yourself there is an Action Controller Helper called View Renderer it does your job for you all you need to follow is its naming convention i.e if your controller name is 'BookController' then its view file should be located at views/scripts/book/boo.phtml .
Assuming BookController extends Zend_Controller, it should automatically setup the view and you shouldn't have to render it. Your view file should be in /application/views/scripts/book/book.phtml. Follow the quick start for more information.
I'm defining a partner through a route based on the url e.g.
my.domain.com/:partner/:controller/:action
Now I want load the config file, databases for the partner before the front controller is called.
Where do I locate this code?
How do I get/set the variables/db, that they are later available in the controller?
I know I could do this through a controller helper but I guess this is not the best point to do it?
Yes, a controller plugin is the way I'd do it:
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
switch($request->getParam('partner')) {
//... do something based on the possibility
}
}
}
I have a situation where T4MVC is generating everything properly (meaning intellisense shows all areas/controllers/actions and everything compiles), but when I run the code, I get a T4MVC was called incorrectly runtime error.
I've investigated the generated files and discovered that for one controller in my project, only actions in the base class are getting the overridden stub actions generated. For other controllers, all actions are being generated. They all have the same set up, described below.
I have a BaseController class that has some shared code (and inherits from Controller). In the Controllers directory (root of project) I have a number of controllers, all which inherit from BaseController.
I then have several Areas. In each Area, I have the same controllers, each inheriting from the controller of the same name in the root Controllers directory.
Running T4MVC (version 2.6.54), everything works fine except for one controller. The odd thing is that intellisense works for the controller, but chokes when the actual action is referenced (in an ActionLink() call).
I manually added one action in particular into the generated code and there was no error.
So my question is, what would cause T4MVC to not generate all code for a controller? The missing actions are all public virtual ActionResult and the actions themselves work fine. The problem controller has the same issue in all Areas.
Some abbreviated code.
/Controllers/BaseController.cs
namespace MyProject.Controllers
{
public abstract partial class BaseController : Controller
{
protected ISession session;
public BaseController()
{
}
// other shared methods/actions
}
}
/Controllers/ActivitiesController.cs (this is the problem controller)
namespace MyProject.Controllers
{
public partial class ActivitiesController : BaseController
{
// for resolving concurrency exceptions
private Activity userValues;
private Activity databaseValues;
public ActivitiesController() : base()
{
ViewBag.ControllerName = "Activities";
}
// this action is causing the problem used like
<li>#Html.ActionLink("Activities", MVC.Areas.Module1.Activities.Index())</li> in a view
public virtual ActionResult Index()
{
return View();
}
}
}
/Areas/Module1/Controllers/ActivitiesController.cs. This is the whole class
namespace MyProject.Areas.Module1.Controllers
{
public partial class ActivitiesController : MyProject.Controllers.ActivitiesController
{
public ActivitiesController() : base()
{
base.currentModule = Modules.Module1;
}
}
}
In case anyone else comes across this I had a similar issue and resulting run-time error message but in a bit different scenario. It was in the RedirectToAction statement at end of a ActionResult method:
RedirectToAction(Edit(id));
The error went away after correcting it to:
RedirectToAction(MVC.[action name].Edit(id));
The error message isn't very intuitive and the suggestion to re-run the custom tool doesn't really help.
Did you make sure to re-run T4MVC to generate based on the latest (Right click .tt file / run custom tool)?
If that's not the problem, I may need to look at a sample app that has the problem to see what's going on.