I am creating some hard coded routes that will likely be changed again freely in the future. To abstract the ideas a bit:
We have a controller/method BuySubscriptionController#start:
class BuySubscriptionController
function start()
{
$plan = Plan::findBySlug($request->get('plan'));
return view('someView', ['plan' => $plan]);
}
}
We currently have the following route:
Route::get('/buy-subscription/start', 'BuySubscriptionController#start');
This means the sales team would need to advertise the following urls:
site.com/buy-subscription/start?plan=plan-one
site.com/buy-subscription/start?plan=plan-two
Now we have been requested to have a few specialized routes:
site.com/purchase/the-basic-plan (plan-one)
site.com/purchase/the-mega-plan (plan-two)
Now I am trying to add these specialized urls to my routes. I was hoping to do something as follows, but does not work:
Route::get('/purchase/the-basic-plan', [
'uses' => 'BuySubscriptionController#start',
'with' => ['plan' => 'plan-one']
]);
Route::get('/purchase/the-mega-plan', [
'uses' => 'BuySubscriptionController#start',
'with' => ['plan' => 'plan-two']
]);
Is there any way to achieve this, simply, without over engineering some new translation layer? Keep in mind that next week the url might be /buy/the-god-plan meaning plan-one, so being able to simple add a line to my routes seems ideal.
You can define a route that takes the plan as a parameter, and use Regular Expression Constraints so that parameter can only take certain values that you allow. So your route definition can look like this:
Route::get('/purchase/{plan}', 'BuySubscriptionController#start')
->name('purchase-plan')
->where('plan', 'the-basic-plan|the-mega-plan');
Then in your controller action just use the parameter:
class BuySubscriptionController
{
protected $plans = [
'the-basic-plan' => 'plan-one',
'the-mega-plan' => 'plan-two'
];
function start($plan)
{
// You can use an associative array to convert the $plan parameter
// into the value you need for querying the database
$plan = Plan::findBySlug($this->plans[$plan]);
return view('someView', ['plan' => $plan]);
}
}
If you need to generate URLs for the route you can just use the route helper method and pass it the plan name:
route('purchase-plan', 'the-basic-plan');
And you'll get:
site.com/purchase/the-basic-plan
This solution allows you to add any number of plan names by just adding the public plan for the URL in the where constraint of the route, then associating that value with the one you need for the query in your controller's $plans property.
Related
I am using a Laravel jenssengers MongoDB and it is my first time seeing the word obfuscating. I see many open source options on github but nothing that does what i want.
How do i obfuscate my url from looking like this
http://blog.dev:8000/article/57780ee99a892008f64d5341
To looking like
http://blog.dev:8000/article/how-to-do-this-article
My routes expect an article id like so
Route::get('/article/{article_id}', [
'uses' => 'MainController#singleArticle',
'as' => 'article'
]);
...And Controller
public function singleArticle($article_id){
$article = Article::find($article_id);
return view('article',['article' => $article]);
}
And i am assuming it is quicker working with ids rather than text.(Correct me if i am wrong) Therefore changing the route to ...
Route::get('/article/{article_title}', [
'uses' => 'MainController#singleArticle',
'as' => 'article'
]);
and controller to
public function singleArticle($article_title){
$article = Article::where('title',$article_title)->first();
return view('article',['article' => $article]);
}
Would be a bad choice that i might regret later when two articles have the same title?
So how do i obfuscate my routes, to look like the title of the article which is being fetched from the database?
I've declared this route:
Route::get('category/{id}{query}{sortOrder}',['as'=>'sorting','uses'=>'CategoryController#searchByField'])->where(['id'=>'[0-9]+','query'=>'price|recent','sortOrder'=>'asc|desc']);
I want to get this in url: http://category/1?field=recent&order=desc
How to achieve this?
if you have other parameters in url you can use;
request()->fullUrlWithQuery(["sort"=>"desc"])
Query strings shouldn't be defined in your route as the query string isn't part of the URI.
To access the query string you should use the request object. $request->query() will return an array of all query parameters. You may also use it as such to return a single query param $request->query('key')
class MyController extends Controller
{
public function getAction(\Illuminate\Http\Request $request)
{
dd($request->query());
}
}
You route would then be as such
Route::get('/category/{id}');
Edit for comments:
To generate a URL you may still use the URL generator within Laravel, just supply an array of the query params you wish to be generated with the URL.
url('route', ['query' => 'recent', 'order' => 'desc']);
Route::get('category/{id}/{query}/{sortOrder}', [
'as' => 'sorting',
'uses' => 'CategoryController#searchByField'
])->where([
'id' => '[0-9]+',
'query' => 'price|recent',
'sortOrder' => 'asc|desc'
]);
And your url should looks like this: http://category/1/recent/asc. Also you need a proper .htaccess file in public directory. Without .htaccess file, your url should be look like http://category/?q=1/recent/asc. But I'm not sure about $_GET parameter (?q=).
How can i configure a route connection to handle...
/users/{nameofuser_as_param}/{action}.json?limit_as_param=20&offset_as_param=20&order_as_param=created_at
in the routes.php file such that it calls my controller action like...
/users/{action}/{nameofuser_as_param}/{limit_as_param}/{offset_as_param}/{order_as_param}.json?
Note: Iam using Cakephp 2.X
to handle...
/users/{nameofuser_as_param}/{action}.json
That's pretty easy, and in the docs.
Assuming there is a call to parseExtensions in the route file, a route along the lines of this is required:
Router::connect(
'/users/:username/:action',
['controller' => 'users'],
[
'pass' => ['username'],
// 'username' => '[a-Z0-9]+' // optional param pattern
]
);
The pass key in the 3rd argument to Router::connect is used to specify which of the route parameters should be passed to the controller action. In this case the username will be passed.
For the rest of the requirements in the question it would make more sense for the action to simply access the get arguments. E.g.:
public function view($user)
{
$defaults = [
'limit_as_param' => 0,
'offset_as_param' => 0,
'order_as_param' => ''
];
$args = array_intersect_key($this->request->query, $defaults) + $defaults;
...
}
It is not possible, without probably significant changes or hacks, to make routes do anything with get arguments since at run time they are only passed the path to determine which is the matching route.
I have this defined in my routes.php file
Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
And in my login.blade.php file, the form starts as this
{{ Form::open(array('route'=>'Loguearse'))}}
I dont know why when i submit the form takes the second route instead the first one, even though I am pointing to the first one.
There must be a way to go to the same url from two different forms, that is what I want.
If you have two routes with the exact same URI and same method:
Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
How can Laravel know the difference between them when something hit /gestionAdministrador?
It will always assume the first one.
The name you set 'as' => 'RegistrarAdministrador' will be used to create URLs based on that route name, only, when something (browser, curl...) hit the URL the only ways to differentiate them is by
1) URL
2) URL parameters (which is basically number 1 plus parameters)
3) Method (GET, POST)
So you could change them to something like:
Route::post('gestionAdministrador/loguearse', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador/registrar', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
EDIT 2
What you really need to understand is that the name you give to a route ('as' => 'name') will not be part of your url, so this is not something that Laravel can use to differentiate your two URls, this is for internal use only, to identify your routes during the creation of URLs. So, those instructions:
$loguearse = URL::route('Loguearse');
$registrar = URL::route('RegistrarAdministrador');
Would generate exactly the same URL:
http://yourserver.dev/gestionAdministrador
EDIT 1 - TO ANSWER A COMMENT
Redirecting in Laravel is easy, in your controller, after processing your form, in any of your methods you can just:
return Redirect::to('/');
or
return Redirect::route('home');
Having a route like this one:
Route::get('/', array('as' => 'home', 'uses' => 'HomeController#index'));
So, your controller would look like this:
class AdministradorController extends Controller {
public function RegistrarAdministrador()
{
...
return Redirect::route('home');
}
public function Login()
{
...
return Redirect::route('home');
}
}
Actually you have only one route in your route collection, because:
You have following routes declared:
Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
Both of these used post method and this is post method:
public function post($uri, $action)
{
return $this->addRoute('POST', $uri, $action);
}
It calls addRoute and here it is:
protected function addRoute($methods, $uri, $action)
{
return $this->routes->add($this->createRoute($methods, $uri, $action));
}
Here $this->routes->add means Illuminate\Routing\RouteCollection::add() and the add() method calls addToCollections() and it is as follows:
protected function addToCollections($route)
{
foreach ($route->methods() as $method)
{
$this->routes[$method][$route->domain().$route->getUri()] = $route;
}
$this->allRoutes[$method.$route->domain().$route->getUri()] = $route;
}
The $routes is an array (protected $routes = array();) and it's obvious that routes are grouped by methods (GET/POST etc) and in each method only one unique URL could be available because it's something like this:
$routes['post']['someUrl'] = 'a route';
$routes['post']['someUrl'] = 'a route';
So, in your case, the last one is replacing the first one and in this case you may use different methods to declare two routes using same URL so it would be in different array, something like this:
$routes['post']['someUrl'] = 'a route';
$routes['put']['someUrl'] = 'a route'; // Route::put(...)
There must be a way to go to the same url from two different forms
Yes, there is a way and it's simply that you have to use the same route as the action of your form and therefore, you don't need to declare it twice.
What you want to do is a bad idea, you shouldn't be logging in and registering from the same route. With that said what you are saying isn't really possible. Routing in Laravel is first come first served. Basically it checks the route until the URI matches one and then calls that method on the controller or executes the callback. Your routes have to be the other way in your routes file. This will be fixed by changing the url.
I was wondering if there was an easy and best practices way to make routes in CakePHP (routes.php file) to map userIDs to a vanity url?
I have (terrible way to do this) the following test code in my routes page:
$users = array
(
1 => 'firstname-lastname',
2 => 'firstname2-lastname2'
);
//profiles
foreach($users as $k => $v)
{
// LESSONS (Profiles)
Router::connect('/:user', array('controller' => 'teachers', 'action' => 'contentProfile', $k),
array('user' => '(?i:'.$v.')'));
}
The above code routes my teachers controller with conProfile as the action from:
mydomain.com/teachers/contentProfile/1
to
mydomain.com/firstname-lastname
Can I connect to the db from the routing page? Is that not a good idea in terms of performance? Let me know what's the best way to do this.
You can create a custom route class that will look up passed urls in the database and translate them to the correct user id. Setting a long cache time should mitigate any performance impact of hitting the DB.
The book documentation is a little thin, however, but the basic structure is this:
class TeachersRoute extends CakeRoute {
/**
* Modify incoming parameters so that controller receives the correct data
*/
function parse($url) {
$params = parent::parse($url);
// Add / modify parameter information
// The teacher id should be sent as the first value in the $params['pass'] array
return $params;
// Or return false if lookup failed
}
/**
* Modify parameters so calls like HtmlHelper::url() output the correct value
*/
function match($url) {
// modify parameters
// add $url['slug'] if only id provided
return parent::match($url);
}
And then in your routes:
Router::connect(
'/:slug',
array(
'controller' => 'teachers',
'action' => 'contentProfile'
),
array(
'slug' => '[a-zA-Z0-9_-]+'
'routeClass' => 'TeachersRoute',
)
);