I'm wondering if there is a way to declare routes in MVC3 so that the route "zone1/{controller}/{action}" would direct to {controller}.zone1{action} method and "zone2/{controller}/{action}" would direct to {controller}.zone2{action} method, for example. So that's basically transforming the target action name based on the route.
Thanks
Check out The Attribute Routing project. You can specify on your methods the routes which I feel is a bit easier to read. Here's a decent blurb on it:
http://gregorsuttie.com/2012/01/12/attributerouting-for-mvc/
You could also write your own custom route handler but I don't believe you can do what you want without some custom code. I could be wrong here though. The attribute routing project should work just fine for what you want however.
Related
based on my design requirements, I will like to exclude the suffix 'Controller' from my controllers and replace it with 'Resource'. So that 'FundsController' will become 'FundsResource'.
The problem is that I am not able to route to my specified actions by either convention based or attribute routing when I change replace the term 'Controller' and get an error saying that the controller with this name could not be found.
How can I satisfy the above mentioned design requirement and also be able to route without a problem? Either in Convention based or Attribute routing? Or whether if we can merge the benefits of convention based and attribute routing to achieve this?
Thanks in advance.
"Controller" is hard-coded into the framework but you can you can create a new controller type resolver. Here is an excellent article from Filip - http://www.strathweb.com/2013/02/but-i-dont-want-to-call-web-api-controllers-controller/.
So, I've been able to get restful controllers working with
Route::controller('users','UserController');
class UserController extends BaseController {
public function getAccount(){}
}
and so /users/account works. However if I try to do something like
Route::any('account',array('as' => 'account','uses' => 'UserController#account'));
and go to /account, it doesn't work (NotFoundHTTPException). Is there a way to use named routes and restful controllers in conjunction? I like how the restful system breaks up requests, and how named routes encapsulate the URI's and decouple them from the function names. This worked in Laravel 3. Am I missing something in the syntax, or did Laravel 4 purposefully disallow this kind of mix-and-match behavior? Thanks...
This would depend entirely on the order you have defined the routes. If it's not working try reversing the order of the definitions.
But because Laravel is all about making your life easier you can pass an array of method names and their corresponding route name as the third parameter to Route::controller.
Route::controller('users', 'UsersController', ['getProfile' => 'user.profile']);
This might not directly apply to your situation but it is super handy.
Try this:
Route::get('/',array('as'=>'named_route','uses'=>'yourRestfulController#getMethod'));
This works nice for me. The trick was adding the action type after # part. You should use the full name of the method unlike in L3.
This works nice for me. The trick was adding the action type after # part. You should use the full name of the method unlike in L3.
Because REST prefix get, post, and so on are patterns to distinguish what type of REST it implements. When you named restful controllers route they didn't act like RESTful controllers anymore but a normal Controller you wish to named. Example of this:
Route::get('user/profile/', array('as'=>'dashboard', 'uses'=>'ProfileController#showDashboard'));
Consider this one:
Assuming we want SystemController to be a RESTful controller so you'll define:
Route::controller('/', 'SystemController');
Then you want to named the postDashboard on the SystemController as dashboard, so you'll modified your routes as:
Route::get('user/profile/', array('as'=>'dashboard','uses'=>'SystemController#postDashboard'));
Route::controller('/', 'SystemController');
On that scenario,postDashboard should not be access via GET protocol since we declared it to bePOST, that is if Laravel treated it as RESTful Controller, since we named it that way it will be treated as normal not RESTful, so we can access it tru GET protocol. Naming it that way will be so dramatically not appropriate, coz we are breaking what we want first which is telling Laravel to treat SystemController as a RESTful.
I think you have to consider the post of Jason Lewis as the appropriate answer. No hard feelings #arda, since you are also correct.
Is it possible to call directly from a model method from another model of the same component?
Is there any default Joomla option to call in such a way.
Yes You can
It will not break MVC architecture,
You can check like this
if(!class_exists('VirtueMartModelUser')) require(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'user.php');
$usermodel = VmModel::getModel('user');
$currentVMuser = $usermodel->getUser();
First you should include the model file in the required model then create the object.
then call like above.
This example is Virtue-mart using method
According to the my knowledge NO. Otherwise it will break the MVC architecture.
What you can do is
Replicate the function you want to use.
Make the call from the controller to the both method.
My Advice is to you is even if you figure out a way to do it, Don't do it. It will mess up your whole architecture.
If you have any issues please ask.
I'd like to be able to use T4MVC with custom routes.
Is this possible and how do I use it? I've not seen anything generated in the strong helpers to be able to do this. Since the custom routes are just strings, I don't know if this is even possible.
If by custom route you mean 'named routes', then T4MVC indeed doesn't generate anything for them, as that would require parsing your code.
Instead, the simple thing to do is to just define a public constant with the route name, and use that both in the route definition and wherever you need to refer to the named route.
using CodeIgniter normally one has to specify the controllers in the config/routes.php file.
This is not to handy, so I would like to be able to do something like this in a controller.
get url parts and check if the first part is specified in an array
if so, load the specified controller, if not, load default controller.
It basically mimics the behavior of the routes file, but there is no need to specify the wildcards before. I am using a base controller I extend with every controller, but I would like to have this controller just load (or include) the needed controller.
Does anyone have any idea how I can do this in a good way?
Thanks in advance.
// Edit
Okay, so here is my scenario.
I have cms and users can choose to include modules (e.g. a gallery).
I need to inlcude all the gallery php scripts without having to have "gallery" in the url.
I figured it would work if I use a "main controller" which loads another controller depending on the modules chosen. I realize this might not be the best way, so if there is a "clean" way to do it, please tell me.
As far as I know models are just for database stuff, so putting a whole gallery in there is not right either. The Plugin itself will of course be a library, but there is going to be some code to load the libs depending on the demands, get the database data, etc.
Thanks
The way you're doing this is incorrect. You never should take over the routing function to do this. What you need is to use some kind of module functionality to include the required methods and models; a module doesn't require to have route-accessible methods, so it's basically a "library" with a model and views.
If I remember correctly there are several plugins that provide this, One was HMVC (google it).
The ideal form would be to load the "Module" on demand from your controller, like you do with the core CodeIgniter Libraries; so lets say you're inside the blog controller action and you want to include the comment module which is used on gallery and on images, you just include the module and call it's methods to get the data which you can then pass to the view as needed; you can even render partials and store them into a variable to pass to your master controller.
Hope this is enough to get you on the right track :)
I may be misunderstanding your question but you want to load your controllers if you go to them and if not you want to go to your default.
If I am understanding correctly you can do a couple things in your routes have a route that takes everything to your default controller.
In your controller have an array of all your controllers then implode the array to a regular expression
$array = [c1, c2, c3, c4];
$str = implode('|', $array);
$regex = "($str)"
now just add your regex to a route
now redirect as you see fit.
But this is really what the routes file is for you you are just dancing around something that should be used.