Is there a Laravel route link Angularjs/otherwise? - angularjs

I'm new to Laravel. As I asked in title, is there any route declaration in Laravel like AngularJS(otherwise) ? For example, I'll define some routes and all other URI requests will redirect to a certain page/index page/ error page ?

Your question is a little unclear, but it sounds like you're looking for a catch all route.
According to the Laravel Snippets sites, if you add the following to the end of your app/routes.php, this should give you what you want.
Route::get('{slug?}', function(){
return 'Catch All';
})->where('slug', '.+');

You clearly didn't even try to Google this: laravel routes.
http://laravel.com/docs/4.2/routing

In your Laravel application, locate to app/routes.php
That's where your routes are kept. You can define them like so:
Route::get('/home', function() {
return View::make('pages.home');
});
Or to call a controller:
Route::get('/home', ['uses' => 'HomeController#index']);
You can call HTTP verbs as well, like so:
Route::post('/some/form', ['uses' => 'YourController#process']);
Please see here for more info: http://laravel.com/docs/4.2/routing

Related

Disabling routes in angular js + laravel project

I have a laravel application which uses angular js as the front end. There I need to disable this route. appo.dev/ which means the root path of the application. How can I disable that path only. Here I need to access other routes such as appo.dev/progess. I tried the following way in routes.php file. It's better if I can find a solution with this code.
Route::any('{path?}', function () {
return view("appo_app");
})->where("path", ".+")
->whereNotIn("path", "appo.dev/");
Above without whereNotIn clause it will work for all the routes. So I am thinking a way to disable only that particular route via wherenotin clause. Or is there a better wild card character? Anyone knows how to solve this issue.
Maybe without what you want is like the root route did not exist, you can try this.
And it will return an error 404. Maybe that's what you're looking for?
I assume that appo.dev is the domain.
Route::any('/', function () {
abort(404);
});
Route::any('{path?}', function () {
return view("appo_app");
})->where("path", ".+");
If it were not the domain, maybe this will help you.
Route::any('appo.dev', function () {
abort(404);
});
Route::any('appo.dev/{path?}', function () {
return view("appo_app");
})->where("path", ".+");
I hope it helps you. A cordial greeting.

How Flask default route avoid redirect?

I hava a list page:
$routeProvider.when('/', {
redirectTo:'/1', // so default will be page 1
}).when('/:page',{
controller:'listCtrl',
templateUrl:'xxxxx'
})
In the controller will use $route.current.params.page to do the logic.
The question is:redirectTo will cause a browser 301 which I don't want.(bad performance) And I also don't want everytime one click the main nav button, it would cause a 301.
How can I archive the same goal without use redirectTo?
It's something like: if I use the route same as '/:page'(write the same controller / templateUrl), how can I pass the page number(1) to the controller without route param?
EDIT:
Thanks to the accepted answer.
Changed the title to Flask related.
Found that the 301 was caused by Flask's defaults route:
#bp.route('/list', defaults={'page':1})
#bp.route('/list/<int:page>')
def lst(page):
when the request uri is /list/1, will be 301 to /list... don't know how to resolve yet, but this is another question.
redirectTo in $route doesn't actually do an HTTP redirect - it just redirects logically to the right ng-view. So, there isn't any performance hit.
But just for completeness-sake, you can pass a parameter in the resolve property:
.when('/:page?',{
controller:'listCtrl',
templateUrl:'xxxxx',
resolve: {
page: function($route) { return $route.current.params.page || 1; }
}
})

Laravel RESTful returning 301 state

Mates,
I'm developing a Backbone/Laravel application.
When I try to send a POST request to a certain resource, it returns me 301 status with no response, regardless that on the controller i'm printing a 'hello world', to check if it's getting to that point.
Here's some code...
public function store()
{
//
return 'hello world';
}
This is the routes.php
Route::group(array('before' => 'auth'), function()
{
Route::get('/', 'SitesController#index');
Route::resource('rooms', 'RoomsController');
});
So, when I make a POST request to
rooms
With some info to save, it gives me back 301 status with no response at all.
Any idea what am I doing wrong?
Thanks in advance!
Solved!
On backbone collection configuration, I had url parameter with '/' at the end.
rooms/
Deleted that slash and now it works fine.
Hope someone find's this helpful
(Adding this answer in case anyone has the same issue as me)
In my case I had a resource videos which gave the exact same problem. The reason was that I also had a directory on the server /videos/ (with files in it) which the server tried to point me to before it let Laravel direct it to the controller.
For some reason, posting to "image" in Laravel, even without a trailing "/" gives me a 301 Moved Permanently HTTP response.
I don't have an "image" folder in my public directory and still don't understand why the redirect occurs.
Changing the request to "foo" seems to work fine.
So if you found this page because you're trying to store an image restfully, try something like this:
Route::resource('api/image', 'ImageController');

Router in backbone not handling route after initialization

I have multiple routers in my app, in general way it looks like this:
// Start backbone.js
if (!Backbone.History.started) {
Backbone.history.start({pushState: true, hashChange: false});
}
// Perform some RPC requests ...
// Depending on user role, received from the server should be created suitable router:
var router;
if (typeof app.user.role === 'manager') {
router = new routers.manager();
} else {
router = new routers.guest();
}
Problem is that after page is loaded and script is executed routers do not do. anything. They do not load route for current url automatically. So, i had to fix it this way (i am not sure that it is a right way):
routers.guest.initialize = routers.manager.initialize = function() {
var defaultRoute = 'default';
if (typeof this.routes[Backbone.history.fragment] !== 'undefined') {
this[this.routes[Backbone.history.fragment]]();
} else {
this.navigate(defaultRoute, true);
}
};
It is working fine, except one bug: when i use route with params, for example /reset-password-confirm/:code - it is unable to find in in routes property. I could write some more code to fix it, but i suppose that i am doing something wrong, if i have to write such things - as i understand router should handle routes just after it was created.
So, questions:
Why my router(s) does not handle routes for current url after it is being created? Perhaps i need to start backbone history later? (but this bug will happen again later then)
How it is possible to make routes with params like /user/:id work there?
Perhaps it is bad idea to re-create routers? Perhaps it is better to create all of them one time?
P.S. I've tried to create both routers and keep them, also i've trie to call backbone history start method after all routers were created.. but this didn't help :/
Assuming you route is declared as the following:
routes : {
'/user/:id' : 'user'
}
Your initialize code is not working because when you initialize your router with a url such as: /user/1234. Backbone.history.fragment will be /user/1234 (not /user/:id). Since the this.routes object doesn't have a key of /user/1234, your else clause calls the default route.
If you first instantiate your router then call Backbone.history.start(), you will be able to remove your router initialize code. When you navigate to a url as /user/1234 your router will match the /user/:id route and call the user function.
The following should work for you without adding your initialize code:
var router = (app.user.role === 'manager') ? new routers.manager()
: new routers.guest();
Backbone.history.start({pushState: true, hashChange: false});
Looking at the code, seems like you're starting the backbone history before initializing any routes. That's most likely not goning to work.
The correct way of doing this type of seperation is by creating all the routes based on the role received from the server and then start the backbone history. Here's an SO thread that talks about it with code samples as well : How to protect routes for different user groups

Laravel full URL routing

How can I set my routes depending on the domain-name? I want to register some actions to diffenrent domain-names (not sub-domains).
Example of the functionality I need to replicate:
Route::any('www.domain1.com', 'Controler#Action1');
Route::any('www.domain2.com', 'Controler#Action2');
I can't use URL rewriting in .htaccess because I store domain->route maping in my database.
i think you can do it like this
Route::group(array('domain'=>'www.domain1.com'), function(){
Route::get('/',array('as'=>'domain1Home', 'uses'=>'Controller#Action1'));
});
Route::group(array('domain'=>'www.domain2.com'), function(){
Route::get('/',array('as'=>'domain2Home', 'uses'=>'Controller#Action2'));
});
you can know more about that from http://laravel.com/docs/routing#sub-domain-routing its some how the same way of thinking ..

Resources