1

I am trying to avoid this code:

Route::get('/', array('as' => 'create_content', 'uses' => 'SameController@create'));
Route::get('create_content', array('as' => 'create_content', 'uses' => 'SameController@create'));

and combined them into one route. because the controller is the same.

so this controller will be active when u go to the index "/" and to "create_content".

And if i am already here - maybe someone can elaborate for me about the purpose of "as" in the array?

Thanks!

2 Answers 2

3

In Laravel you're not restricted to static values for your paths. You can also use {}'s to denote a variable. Adding a ? after the variable name makes that variable optional. These variables are passed to your routing method (in this case, the "create" method of SameController).

This can be combined with the where method to pull off what you want. The where method allows you to define RegEx restrictions on what's allowed by a variable.

Another alternative is to use a Route::pattern, which is basically a more "global" version of where(). I've included an example of both for your convenience. :)

As for 'as', you're able to name a route in Laravel. After naming a route with 'as', you can access it through some of Laravel's useful functions, such as URL::route('nameOfRoute') or Redirect::route('nameOfRoute');

A functioning example is below:

Route::pattern('myPattern', '(create_content)?');

Route::get('/{path?}', array('as' => 'nameOfRoute', 'uses' => 'SameController@create')
)->where('path', '(create_content)?');

// This is functionally equivalent to the above Route::get.
// Route::get('/{myPattern?}', array('as' => 'nameOfRoute', 'uses' => 'SameController@create')
// );

// Example of how to make use of the 'as' defined above.
Route::get('create_more_content', function() {
    return Redirect::route('nameOfRoute');
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is a nice solution, IMO and as used to use that route with name assigned to as so you can refer to that rout using the route name like:route('create_content') instead of to('/').
0

Despite you cannot avoid that code, because of the following reason. As it is the same route to the same controller, the request is still different.

So i think you have two options...

1) Go with the above code

or

2) hack your .htaccess file and redirect it to / when the request is create_content

And believe me, the first is the most easiest way.

The question you need to ask is, do i need that second route. Is there content spreaded like url's that point to that location ? If not that much i would drop the second entirely.

3 Comments

Thanks for the comment! i just saw something like i want to do been used in old tutorial (l3 i believe) so i was sure there is a good way of doing it in l4. Route::get('/, new_content', array('as ...
in l4 it didn't work for me. but according to the tutorial (tuts plus 29-saving and displaying snippets) it dose in l3 i believe.
This is what is in the api... public Route get(string $uri, Closure|array|string $action), so it seems you have to give a string, and right after that comes an array, so i don't think that is supported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.