Kohana 3 Routes for Individual Modules - module

I'm new to Kohana but have been reading the User Guide for about two weeks now. I am building a huge application on v3.3.1 that will have a number of different modules (i.e. residing in the modules directory). I am struggling with getting the routes to work as I need them to.
First, it's worth mentioning that I have read the User Guide about routes, modules, bootstrap etc. I am putting the route::set in the init.php file within the module, so it is being called before the "default" option in the bootstrap.php file. Here's what I have...
Directories:
application > classes > Controller > Welcome.php
modules > module1 > classes > Controller > Home.php
Routes
In module1 init.php file:
Route::set('module1', 'module1(/<controller>(/<action>))')
->defaults(array(
'controller' => 'Home',
'action' => 'index',
));
In bootstrap.php file:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
When I go to localhost/index.php/module1/ it does run the Home controller of the module.
When I go to localhost/index.php it does run the Welcome controller in the application directory.
However, if I go to localhost/index.php/home it also runs the Home controller for the module. I do not want it to do this. I only want the module controller to be run when the URI is localhost/index.php/module1/controller
I know it is the default route picking up the module's controller, but I don't know how to stop it from doing this, or even if it is possible!
If anyone has experience of working with modules in this way please could you help me? You time is very much appreciated.
Many thanks,
Steve

You are using the default route, which is a catch all route. What you should do, is to alter the routes in such a way that your catch all is limited to just the Controllers you have in your main application. This can be done by adding a third parameter with a regex.
Example:
Route::set('module1', 'module1(/<controller>(/<action>))')
->defaults(array(
'controller' => 'Home',
'action' => 'index',
));
Route::set('main-app', '(<controller>(/<action>(/<id>)))',
array(
'controller' => '(welcome|login|posts)'
))
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
If I were you I would even break up the routes further and make specific routes for your Controllers.
Do however note that you cannot use the same Controller names in your main app and in the your modules. With these routes (and probably all), Kohana will always override and use the main app Controller (due to the Cascading File System), even if the route matches the module Controller.
I hope this helps and if you have further questions, just leave a comment.

Related

Change certain controllers inside of yii

So I want my yii framework application to have its User control handling the login and all of that inside a folder named Backoffice.I haven't tried this yet because I know I will brake the website.
So my question is how to tell yii that the files are located under different directories
You can set a path in config/main file. For example
'yiiPath' => dirname (FILE). '/../../framework/yii.php',
or
'import' => array(
'application.models.',
'application.components.',
'application.modules.user.models.',
'application.components.',
'application.modules.role.models.',
'application.modules.profile.models.',
'packages.solr.',
'application.modules.registration.models.',
),

What is the exact Order of execution in yii framework?

What is the exact Order of execution in yii framework?
here, I mean detailed explanation from the moment we enter url in address bar.
How actions in a Controller get executed & when?
If you are talking of basic workflow of yii app then yii definivtive guide can help you.Here is typical workflow taken from the yii definitive guide.
Model-View-Controller (MVC)
If you want the actual workflow of your specific app then you can use logging.
Within a for standard controller, the order of execution for standard methods are:
behaviors,
init,
accessRules,
beforeAction,
Those are the ones I mostly am concerned with.
Enable logging
http://www.yiiframework.com/doc/guide/1.1/en/topics.logging
to see exactly what gets executed and in what order.
If you look at the urlmanager of Yii you will see a bit how it works.
'urlManager' => array(
//somethings
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view', //first rule
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', //second rule
'<controller:\w+>/<action:\w+>' => '<controller>/<action>', //third rule
)
);
After the base url you will have the controller, if the next thing is an integer (<id:\d+>) if will send the request to the view action of the controller. (first rule)
The second rule states that any request with a word and a integer (<action:\w+>/<id:\d+>) will be send to the action with the requested id. For example: http://example/post/update/123 if will be send to the actionUpdate of the PostController with $_GET['id'] == 123.
The third rule is the same as above but with no id. So for example the index and admin action will show a list or a grid with all records

rails routes gives an error - wrong controller seems to be called

I can't figure out what's wrong with my routes. I'm very new to rails (and programming) so it could be something very simple.
Clicking on my home page gives the following error -
Routing Error
No route matches {:action=>"show", :controller=>"bets"}
Try running rake routes for more information on available routes.
This is my routes.rb
root :to => 'pages#home'
resources :bets
resources :bettingevents
get '/bet' => 'bets#index'
get '/bettingevent' => 'bettingevents#index'
The show function in my bets controller has
def show
redirect_to(bets_path)
end
localhost:3000/bets is fine, with create, update, delete, edit routes working correctly. My pages controller has been working perfectly till I added the bets routes.
If i remove resources :bets the homepage works all right, but none of the bets routes work.
What am I doing wrong? Thanks

Using a route helper for a controller without a model in rails

I have a controller JaxDataController for responding to ajax requests which has no associated model.
It has a single routes.rb entry match "/jaxdata/:shape_set_id" => "jax_data#fetch"
I'd like to include the path to this model within a .js.coffee.erb view elsewhere in my app. Are there any routing helpers available for this? Failing that, where should i declare a routing helper to be used in any view?
If you specify the :as option in your route, it will create helpers for that route. Thus:
match "/jaxdata/:shape_set_id" => "jax_data#fetch", :as => :jaxdata
You should then be able to refer to jaxdata_path in your views.
See section 3.6 of this guide: http://guides.rubyonrails.org/routing.html

Rails 3 - index action not loading by default on controller

Now i have a fresh rails 3 install running over rvm 1.9.2
I generated a controller using the follow instruction:
rails generate controller blog index
The output is
create app/controllers/blog_controller.rb
route get "blog/index"
invoke erb
create app/views/blog
create app/views/blog/index.html.erb
invoke test_unit
create test/functional/blog_controller_test.rb
invoke helper
create app/helpers/blog_helper.rb
invoke test_unit
create test/unit/helpers/blog_helper_test.rb
but in browser when i try to get to http://localhost:3000/blog i get:
No route matches "/blog"
but if i type http://localhost:3000/blog/index
it renders the index view.
doesn't it works like Rails 2? where i get to the index view by default with just putting the controller name on the url ?
thanks.
If you look into routes.rb you'll see
get "/blog/index" => "blog#index"
So just remove it with
get "/blog" => "blog#index"
or you can use resources here.
But only question: why do you use singular form? It is nonsensical to call index to singular noun. You should use or "blog#show" as a resource or "blogs#index" as a resources.
Conventions in Rails is a kind of basement. Don't break them if you can follow them
For rails 3:
match '/blog', :controller => 'blog', :action => 'index'
Rails generate does not generate resources for your controller by default. You specified one action for your controller, 'index', so in your case you end up with this in config/routes.rb:
Blog::Application.routes.draw do
get "blog/index"
The simplest thing to do would be to change this to:
get "blog", :to => 'blog#index'
ian.
This is a guess, based on my experience with Rails 2, but here's what I think is happening:
If you'd generated your controller with the scaffold option (that's still in Rails 3, right?), it would have created a model in addition to your controller, and added the corresponding routes via a call to map.resources (or Rails 3 equivalent) - this last bit is what gives you the /models routes you're expecting.
But since you just generated the controller, no model was created, and thus Rails doesn't put in a map.resources statement in routes.rb - map.resources really only makes sense when there's a model underlying your controller. In fact, I don't think it adds any special routes when you generate a controller; you're getting to your index by one of the default routes: /:controller/:action.
So if you want to get to your index from /blog, you'll have to add the route yourself. Luckily, it should be a one-liner.
Hope this helps!
PS: And if you're paranoid, you'll want to disable those default routes before you go to production - they allow GET requests to trigger actions that change your database (e.g. GET:/blog/destroy), opening you up to Cross-Site Request Forgery attacks.
Add this to your routes.rb file match ':controller(/:action(/:id))(.:format)'
It's better if you add it at the bottom of the routes.rb file.
The problem with this approach is that it will make all your actions available through get request. So be careful with that.
Instead of manual routing you can go to /app/controllers/application_controller.rb and add a blank index method
def index
end
make sure your generated controller extends the application controller, and boom all your generated controllers do what you want
Tested on Rails 3.2.*