Laravel Redirect Status Code

Laravel’s documentation doesn’t make this obvious, but when you are using a redirect you can change what the status code of the redirect is.

Route::get('some/route', function(){
    return Redirect::to("another/route", 301);
});
Route::get('some/route', function(){
    return Redirect::route("another.route", null, 301);
});

The tricky part is knowing which parameter is the status code. To check which parameter it is, refer to the Redirector Class.

Strive For Success

Life is hard. It’s one of those clichés which happens to be true. Despite this, it’s always better to strive for success than to accept defeat. I know this because I have lived it.

Growing up, I’d always been an exemplary student. I was on track to being able to graduate high school early, despite my lifelong struggle with ADHD. That changed at age 17, when my bipolar disorder symptoms fully manifested themselves. Suddenly, by the end of my senior year, I found myself two classes short of being able to graduate and had to get them through special education.

This struggle continued into college. Due to my psychiatric difficulties, I chose to take only two classes, to see if I could get my feet back under me and get on with my life. Instead, besieged by my difficulties, I continued to struggle with completing assignments and passing tests, things which previously had not been an issue for me. By the middle of the semester, I was in danger of flunking both of the two classes. My social worker told me that my only chance of salvaging this mess was to sacrifice one of the classes in order to focus solely on the other.

Needless to say, I was distraught, discouraged, dismayed. I thought I could get things going again, but instead here I was being told to give up. I knew in my heart that, if I did that, I’d likely never be able to recover from the psychological blow of the failure. How could I possibly pursue my dreams if I didn’t even have the strength to pass two measly college classes? At the same time, from the position I was in, I’d basically have to have a perfect second semester to get good grades out of both of those classes, a very stringent task.

My choice was clear: give up and accept a mediocre life, or fight for the chance to succeed.

I chose to fight.

From that point on, I redoubled my scholastic efforts. I went back to my textbooks and forced myself to read each and every page, no matter how mundane. I poured myself into my projects, determined to overkill with quality. I made sure to be well-prepared for finals. It was a long, difficult struggle.

That struggle was rewarded; when the dust settled and the semester was over, I had passed both classes. One was a B, the other an A. This, when I was flunking both of these classes mid-semester. The biggest success, however, was mental: I had proven that I am still capable of pursuing my dreams. The struggle didn’t end there, of course, but I consider that point in time to be the moment where I reversed the decline my mental health had imposed on my life.

Since then, I continued for awhile in college. Ultimately, I decided to drop out and focus on learning how to work. From there, I taught myself how to code, and now I am blessed with a job which is able to support myself, my wife, and my child. None of this would have happened had I chose to give up and accept mediocrity.

If you’re struggling with the difficulties of life, especially if you’re dealing with mental disabilities, don’t give up. Keep fighting!

Wildcards in Laravel Routes

Laravel doesn’t have a way to use wildcards in routes by default. However, you can add one in by using route parameters and regex constraints. Don’t worry: it’s super-easy to set up!

Start by picking a term to use as the wildcard (for these examples, I chose “whatever”). Insert that term as a route parameter wherever you need a wildcard. At the end of your route, chain on the where method to tell Laravel you want to parse a route parameter with a regex. The first argument is the term you’ve chosen; the second argument is .+, which will select everything in the route parameter.

Route::get('some/route/{whatever}', function(){
    // do your stuff here
})->where('whatever', '.+');

Voila! You’ve just created a route wildcard!

There are a variety of uses for this. One is if you have a bunch of old links which start with the same prefix, but don’t end in an identifiable pattern. Another is if, instead of returning a 404 error for non-existing routes, you want to take some other action (like a redirect back to the home page).

You aren’t limited to just catching whatever’s on the end of your url, either. You can stick the wildcard in the middle of the url and it will catch everything in between!

// Catch anything starting with /some/route and ending with /after

Route::get('some/route/{whatever}/after', function(){
    // do your stuff here
})->where('whatever', '.+');

There are a couple of catches you should be aware of. One is that you can’t use a wildcard and another route parameter right next to each other in the same slash block.

// This won't work.

Route::get('some/route/{param}{whatever}', function(){
    // do your stuff here
})->where('whatever', '.+');

Laravel will pick up on {param} and add everything in {whatever} as part of it. To get around this, if you are able, you can add a delimiting character in between the two.

// This works.

Route::get('some/route/{param}.{whatever}', function(){
    // do your stuff here
})->where('whatever', '.+');

Another catch happens when your wildcard parameter doesn’t always show up in the url. For instance, if you want to redirect /some/route and /some/route/, using /some/route{whatever} won’t work because Laravel is expecting something to follow /some/route (the slash is ignored). To compensate, just make the wildcard parameter optional by using a question mark.

// Use a question mark when the wildcard won't always show up in the route.

Route::get('some/route{whatever?}', function(){
    // do your stuff here
})->where('whatever', '.+');

Hope this makes your life a little bit easier!