Cakephp routing not work as my need? - cakephp-3.0

My url is:
http://localhost/rehabhousing/homes/articles/3/terms-conditions
now we routing my url
http://localhost/rehabhousing/terms-conditions
but the url is create dynamicall

at last i can do
$routes->connect('/terms-conditions/*', ['controller' => 'Homes','action' => 'articles',3,'terms-conditions']);

Related

Have same route respond differently based on HTTP verb

The following is a small snippet of my routes in _urlManager.php
["pattern" => 'POST /create_chain', 'route' => 'site/add-chain'],
["pattern" => '/create_chain', 'route' => 'site/create-chain']
As you can see for POST I want a different action to be called. But this does not work.
For now I've used the following solution on temporary basis:
"POST /create_chain" => "site/add-chain",
["pattern" => '/create_chain', 'route' => 'site/create-chain']
But I'm not OK with this solution. If anyone knows how I can integrate HTTP VERB in pattern, please comment or answer.
You should use verb key if you want to configure verb using array syntax:
["pattern" => '/create_chain', 'verb' => 'POST', 'route' => 'site/add-chain'],

Yii2 internationalizating slug and controller

I'm making a yii2 site in 2 languages (for now) using yii's native i18n module, but how can I add multilanguage support for the action URLs?
For instance one of my actions is category/slug and in English, it will show as
http://example.com/category/chair
but in spanish it has to be shown as
http://example.com/categoria/silla
and so on for other languages shown in the future
right now im manually adding my routes like:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<alias:\w+>' => 'site/<alias>',
'marca/<slug>' => 'site/brand',
'categoria/<slug>' => 'site/category',
'brand/<slug>' => 'site/brand',
'category/<slug>' => 'site/category',
],
],
Do i need to manually route every action to its correct controller or is it possible to add a more automated form using yii::t() function?
you will need to write your own UrlRule class, implementing yii\web\UrlRuleInterface and configure your UrlManager.
read more here.
basically it is about "translating" a given request like '/categoria/silla' to your internal url ['site/category', 'slug' => $slug, 'lang' => $lang] in your UrlRule's 'parseRequest' method. 'createUrl' is the other way round.

Yii2 -- Pretty Url -- Domain to Controller/Action with parameters

What will be the rules for my pretty url if I have the following scenario:
links like this where parameters may vary.
domain/?bt=<token>&e=<email>
or
domain/?lt=<token>&e=<email>
then should be processed in a controller/action. ie. mycontroller/get
Also, parameters should be accessible by $_GET inside the action.
the simplest way is based on the use of urlHelper
use yii\helpers\Url;
$myUrl = Url::to(['your_controller/your_action', 'bt' => 123, 'e' => 'myemail#gmail.com']);
Using the urlHelper function Url::to .. the url you need is properly formed depending of the urlManager configuration you have set in your config file
and the param a manager as show in the sample like entry in an array.
The post or get method is related to the type of metho you have in your ulr call if not other values are specified the url is formed as a get
and you can obtain the values you need in $_GET['bt'] and $_get['e']
http://www.yiiframework.com/doc-2.0/yii-helpers-url.html
http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html
http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'' => 'call-backs/get',
'unsubscribes' => 'unsubscribes/get',
],
],
#scaisEdge, thank you for answering my question. maybe my question isn't that clear but this is the solution I made for my question after a hard find of clues and tips online.
All I wanted was that when a user clicks on a link, hitting the main page/main domain, it will go to my yii project (intended to be a webservice or an API like one) then will be handled by a precise controller and action.
'' => 'call-backs/get'
the code above answers the question. Cheers.

CakePHP redirect with form parameter

In CakePHP 3.1 I am redirecting to login page after a database update and I want to pass back the email so that form input is already populated.
I can do it the standard way with using the controller and action in the redirect.
Instead I am using just a url string
return $this->redirect('/login',['?' => ['email' => $email]]);
This gives me the error Unknown status code
The redirect method expects a status code as the second parameter. You will need to provide and array-based URL as the first parameter or append the query var to the current string.
return $this->redirect('/login?email=' . $email);
return $this->redirect([
'controller' => 'Users',
'action' => 'login',
'?' => [
'email' => $email
]
]);

Laravel Response::json not returning JSON properly?

Routes registered in app/routes.php
Route::resource('users', 'UsersController',
array('except' => array('new', 'update')));
Route::post('users/authenticate', array('as' => 'authenticate', 'uses' => 'UsersController#authenticate'));
Route::get('users/is_authenticated', array('as' => 'authenticated', 'uses' => 'UsersController#is_authenticated'));
The method is_authenticated is not returning JSON, but when I put the Response::json() in the index method it returns the JSON schema.
Here is my is_authenticated method:
public function is_authenticated()
{
return Response::json(['authenticated' => Auth::check()]);
}
What's going wrong here? I ran php artisan routes and it returns this for the route:
GET|HEAD api/users/is_authenticated | authenticated | UsersController#is_authenticated
I don't get a 404 Not Found when visiting the page, but there is no content. What's the problem?
Edit: routes are prefixed with api
The problem is that your first route is activated when calling users/is_authenticated. The order of the routes is important in Laravel, as the first matching route is executed. You can just change the order of your routes to make the route users/is_authenticated available, like so:
Route::post('users/authenticate', array('as' => 'authenticate', 'uses' => 'UsersController#authenticate'));
Route::get('users/is_authenticated', array('as' => 'authenticated', 'uses' => 'UsersController#is_authenticated'));
Route::resource('users', 'UsersController', array('except' => array('new', 'update')));