Laravel Routes - is it possible to not use them at all? - laravel

I'm a Laravel 3.x beginner with a CI background.
I'm very acquainted to use controllers rather than routes and I'm having issues trying to use controllers in Laravel.
For example: let's say I have the home_controller and the "about" action. My problem is that I'm only able to access the "about" action by setting a route that points to it - something I think is undesirable.
Is there a way to get the "about" action to work without setting a route?

In laravel, everything can be accomplished using either Routes and/or controllers.
However, using both routes AND controllers is suggested for great flexibility. See this article for more informations and some examples of how to combine routes with controllers.
Anyway, if you want to use controllers (which is perfectly acceptable), you need to register them in your routes.php with Route::controller('yourcontroller') before you can use them.

Everything has to be routed in Laravel. But, you don't have to manually route each method. You could do something along the lines of Route::controller('admin').
See here: http://laravel.com/docs/routing#controller-routing

I like Mike Anthony solution. When you're using only controllers this detect method is everything you have to do - this will register automatically all of your controllers. Best hand free solution so far.
The usual controller registration is, as the guys already mentioned, this:
Route::controller('controllername');

You have to register all controllers like in the example above. It is one line of code per a controller, and it is the rule.
But if you have a static page or a login action (page), a good practice is to create a Route controller (anonymus function), not a classic controller (in controllers folder).

Related

Is it possible to extend Yii URL routes and rules through a Controller?

Is it possible to extend Yii URL routes and rules through a Controller on the fly, or by extending the CController class itself?
I am really stuck with this, have not tried anything.
How to do this in/with Yii?
First you should be aware of what URL rules are used for:
Parsing an incoming URL. The urlManager component resolves a URL to a route so that Yii can call the right action in a specific controller and module.
Creating URLs from calls to createUrl() in your code.
Considering (1) above it is obvious, that you can not add URL rules in your controller if you want to use these URLs in your application. It's much too late, as Yii already has gone through the process of resolving a request to a route. Even if you'd only wanted to create URLs it doesn't make much sense as your application will never understand them.
The correct way to bring more dynamics into your URL parsing/creation is to use a custom URL rule class. In there you can write any code you want to create and parse even the most complex URLs. The topic is described in the manual.

WebAPI - Controller vs ApiController?

So this is kind of a semantic question I suppose. But in my WebAPI project, I have a view that is supposed to allow a user to find resources (it doesnt matter what the resources are). So I created a controller called FindResourceController which is derived from Controller. I then create my view using razor and jQuery. And my controller has a method, called Index which returns this view.
All good so far. But I ALSO want a ApiController to handle jQuery requests from my FindResource view. By convention out of the box, API urls are routed like api/FindResource/, so I should name my api controller FindResourceController and derive it from ApiController. But I already have this class. So then I could name it FindResourceApiControler, but the url maps to api/FindResourceApi/ which seems redundant. This just seems kind of kludgy to me. So either I'm missing something, or this is just the way it is... so, when doing something like this what do you do in your web api project?
Edit: So far, this is all I have found
Link to Article
Is putting the types in a different namespace an option for you? You could have a Controllers namespace and an ApiControllers namespace and have a FindResourceController in each. It might cause some conflicts though so it's not a perfect solution, but it might work for you.

Why use MVC/router

I'm trying to comprehend the concept behind MVC and URL routing. I understand that it's good to seperate your code, hence MVC, but fail to understand the idea behind the URL router!
Instead of having a lot of rewrite rules in htaccess, I send all traffic to router.php, and in this page I have an array with page urls, and its corresponding PHP controller.
To keep it simple, I just include the controller, where the output finally is generated, however having seen lots of other practices, I'm afraid that im doing something wrong, or bad in some way..
Can someone please enlighten me, how to do a good, but simple URL router? Is it okay just to include the controller, which then generates the output? Perhaps someone has some information that describes the subject in details (something understandable for a beginner)
Thanks in advance
There are lots of ways to do URL routing. Some are client side like with backbone.js, others are server side. Doing it with .htaccess is one way, another is th way you are doing it by having a prerequisite path that is is either a hard path, or a regular expression that you parse and figure out where to send it. None of them are 100% right or 100% wrong, it's all preference, and it sounds like you are doing just fine with a route file.
For more information on how different frameworks do routing you should read over the docs on routing for CodeIgniter, and Symfony frameworks to see 2 different styles of server side routing, and then maybe look at the backbone.js framework for client side routing just to see the similarities and differences.
The router in the MVC concept decides which controller it has to load when a user requests a page. E.g. a user requests example.com/something/very/important, the router would now look for an action which is mapped to this route and execute it. There are different methods how you can accomplish that (simple include, instantiating a class and running a method etc.) but the most simple and still powerful solution I came up with is creating a separate class for every action. I've written a little article on that matter, since I've been asked this question several times, you can have a look at it here: Writing a simple and fast mvc router with PHP
The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.
Routing is a pattern matching system that monitor the incoming request and figure out what to do with that request. At runtime, Routing engine use the Route table for matching the incoming request's URL pattern against the URL patterns defined in the Route table. You can register one or more URL patterns to the Route table at Application_Start event.

How to enable gracefull fallback to "root" controllers if controller is not found in area

I have an ASP.NET MVC3 Razor application getting too big to manage effectively.
To have better control over the code I implemented Areas and re-arranged the code to be in the respective areas.
Things that are used from multiple areas are left in the "normal" controller folder.
My problem arises as soon as I use an Action or ActionLink from an area.
I have to manually include a routing parameter called 'area = ""' to have the link work.
There are a lot of links to be changed, so I tried to have the controller selection fallback to the "root" controllers to find the needed controller.
I had no luck so far.
What do I have to do to enable MVC3 to search through the area controllers and continue to search in the root controllers folder if the controller is not found?
Thanks
Andreas
You can do this by setting up your routes appropriately.
Each area has it's own route supplier, and then there are the routes in the global.ascx. The routes act sort of like a case statement, where it tries to find the routes in the areas first, and if it doesn't find a matching route, it then falls through to the global.ascx routes.
So, in your new areas, setup a catchall route, and in the global.ascx setup a catchall route and you should be ok.
You can use Haack's route debugger to see what is happening with your routes.

KohanaPHP Controller. Should I group them?

I'm new to OOPHP and frameworks at all.
I'm just wondering...
I have few controllers:
dashboard
signup
login and few more
I've put them into users directory. Everything is working correctly, I'm just wondering if I should put everything in one controller and signup, etc. should be a method of users controller? Or am I doing it correct way?
Regards,
M
It's totally up to you. The stuff you currently have could probably all go into one controller (user controller in this case), but it can build up to request the separation you already have, e.g. separate controller for each action, grouped by a prefix.
Good thing about kohana is that it allows you to do stuff like this the way you want to, there isn't a single guideline about putting many 'common' actions into the same controller; do it as you like / find appropriate.

Resources