I'm trying to create a plugin that adds an "invite" post type that has an "rewrite" param to prepend "convite" before post slug... The code looks like this:
register_post_type("invite_company", [
'label' => __("Empresas", "modaladvisorsplugin"),
'public' => true,
'supports' => ['title'],
'exclude_from_search' => false,
'show_in_rest' => false,
'rewrite' => [
'slug' => "convite",
],
'menu_icon' => "dashicons-building",
]);
At this point, all works fine. Next step is to add an rewrite rule to append anything after the post slug, that will be used in template as a query var... So, for example, this url:
site.com/convite/company/abcdefg
Must recognize abcdefg as a advisor query parameter. For this, I've tried this way:
register_post_type("invite_company", [
'label' => __("Empresas", "modaladvisorsplugin"),
'public' => true,
'supports' => ['title'],
'exclude_from_search' => false,
'show_in_rest' => false,
'rewrite' => [
'slug' => "convite",
],
'menu_icon' => "dashicons-building",
]);
public function rewrite_rules()
{
add_rewrite_rule(
"convite/([a-z0-9-]+)[/]?$",
"index.php?post_type=invite_company&advisor=\$matches[1]",
"top"
);
}
And filtering query_vars as follow:
public function query_vars($vars)
{
$vars[] = "advisor";
return $vars;
}
Sometime (I don't remember what was in regex), when I tried to access /convite/some-post-slug/abcde, the URL was rewritten to /convite/some-post-slug (excluding the appended segment).
By the way, isn't working... I think is a regex problem (maybe convite/([a-z0-9-]+)[/]?$ isn't what I need), but really I don't know what I'm doing wrong, and I'm not an expert in regex...
Can anyone help me?
I'm not sure if I'm fully understanding the requirements, but this might work better. The regex is a bit easier this way, and it should check for both company by itself as well as company with advisor.
add_rewrite_rule('convite/([^/]+)/([^/]+)/?', 'index.php?invite_company=$matches[1]&advisor=$matches[2]', 'top');
add_rewrite_rule('convite/([^/]+)/?$', 'index.php?invite_company=$matches[1]', 'top');
Also, remember to flush rewrite rules before testing (resave permalinks, or run flush_rewrite_rules(); in your code while testing).
Related
I've made a web service using yii2 basic template I got a table called 'ely_usuario' when I call it with:
http://localhost/basic/web/index.php/ely-usuario/
it works fine and returns me all the rows in ely_usuario table
but when I try to get just one record, for example:
http://localhost/basic/web/index.php/ely-usuario/29
it doesn't work, show me a not found page, I've made the model class using gii
here's my Controller:
<?php
namespace app\controllers;
use yii\rest\ActiveController;
class ElyUsuarioController extends ActiveController
{
public $modelClass = 'app\models\ElyUsuario';
}
My configs:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'ely-usuario'],
],
],
Another weird thing that you might noticed is that 'enableStrictParsing' is false, in the yii2 guide it says to be true but for me it only works with false
Thanks
You need to change the code in your configs.I hope you will get an idea from the following code.It works fine for me!
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:(ely-usuario)>/<action>/<id:\d+>' => '<controller>/<action>',
],
],
And in your controller please check your specific action.It must be coded like as:
public function actionTransactions($id=null){
if($id!=null){
//retrieve single row
}else{
//retrieve multiple rows
}
Also please check this link for reference:
Why RESTfull API request to view return 404 in Yii2?
I hope it helps!
In custom validation as shown below:
'custom' => array(
'attribute-name' => array(
'rule-name' => 'custom-message',
),
),
How is it possible to define regex along with the attribute-name-{regex} as well?!
Example:
'custom' => array(
'institute_regex:{/d{1}}' => array(
'required' => 'the field is required',
),
),
I think that might be what you were looking for ages ago, but anyway, here is what I did.
Had to find a way to define custom validation messages (in another language) for inputs dynamically generated in the frontend. The result is that I would get the following type of classes:
project_name-1, -2, -3 etc to keep it simple.
So what I did, and it worked just fine for me was adding the following lines to my validation.php file (whatever the language is):
'project_name-*' => [
'required' => 'Please name your project.',
],
And voilà, I hope it will help someone!
In my CakePHP, my routes.php file looks like this:
Router::connect('/premios/:category',
array('controller' => 'prizes', 'action' => 'category'),
array(
'category' => '\bmarcas|restaurantes|combustibles|peluqueria\b',
));
This way whenever a user enters the /premios url, the next parameter is matched with the "category" regex. This works perfect.
The problem is that when I want to generate a pretty url for the category, let's say, "peluqueria", using this line of code generates a "non-pretty" url:
Router::url(array('controller' => 'prizes', 'action' => 'category', 'peluqueria')); ?>
Instead of generating a pretty URL (/premios/peluqueria) it generates a non pretty url (/prizes/category/peluqueria).
What am I doing wrong? Or is this a limitation of the Router::url function?
A workaround would be to define every connect, avoiding the regex, but it isn't a pretty as the current solution plus it would get annoying when the categories count start to grow.
Any ideas?
You have to actually name the category parameter in Router::url, like this
Router::url(array('controller' => 'prizes',
'action' => 'category',
'category' => 'peluqueria'));
And you'd probably want to pass that "category" parameter to the category action in Prizes, so you're missing something
Router::connect(
'/premios/:category',
array('controller' => 'prizes', 'action' => 'category'),
array(
'pass' => array('category'),
'category' => '\bmarcas|restaurantes|combustibles|peluqueria\b'
)
);
Look this part of the docs with more detention :)
I have a method in my Users Controller called view, which should display a specified (by URL) user:
public function view($username = null) {
$this->User->username = $username;
if (!$this->User->exists()) {
throw new NotFoundException('Няма такъв потребител!');
}
if (!$username) {
$this->Session->setFlash('Няма такъв потребител!');
$this->redirect(array('action' => 'index'));
}
$this->set('user', $this->User->read());
}
And in the route config:
Router::connect('/:username', array('controller' => 'users', 'action'=> 'view'), array('username' => '^([a-z0-9])+$'));
But when I try: www.example.com/Username it returns a fatal error: Missing controller.
I tried also this:
Router::connect('/users/:username', array('controller' => 'users', 'action'=> 'view'), array('pass' => array('username'), 'username' => '^([a-z0-9])+$'));
Unfortunately for this sort of setup using /:username is too simple, it will pretty much override every single other route. To do this effectively ( + the proper CakePHP way) you need to setup a custom route, here is mine which pretty much achieves the same thing. Just replace "product" with "user" for most cases, read through it though, to make sure you understand what's going on..
Look at my routes config as well if you still can't work it out.
If you want to route /user/dunhamzz to a profile you would set it up like this:
Router::connect('/user/:username',
array('controller' => 'users', 'action' => 'view'),
array('pass' => array('username')
);
Then your view action simply gets the username as the first argument:
public function view($username) {
}
Similar to this question, I have accomplished passing the format with the url extension, but you have have to declare the parameters passed upfront. Example:
new Zend_Controller_Router_Route_Regex(
'([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)(\.(\w+))?',
array(
'module' => 'Default',
'controller' => 'index',
'action' => 'index',
),
array(
1 => 'module',
2 => 'controller',
3 => 'action',
4 => 'extension',
5 => 'format'
)
);
But what if I want /:module/:controller/:action/*.:format???
So that no matter how many parameters I pass through the url, the '.whatever' will be the format parameter? Basically, I'm trying to take the default router and add extension as the 'format param'.
Edit:
The issue isn't grabbing the extension, the issue is adding other params. For example
/blogs/posts/view/post/500/foo/bar/format/html
will translate into:
array('module'=>'blogs','controller'=>'posts', 'action' => 'view', 'post'=>500, 'format' => 'html', 'foo' => 'bar');
But I want to be able to represent the format the same route like so:
/blogs/post/view/post/500/foo/bar.html
No matter how many parameters are declared between the action and the format.
Your whatever you can specify with .* expression.
To match it - simply: (.*)
If you want not to grab for example extension, use ?:
(.*?)(\.(\w+))