I am looking for why I get this error:
ErrorException [ Warning ]: Missing argument 1 for Controller_cee::action_fichestandard()
For this type of URL :
http://localhost/mycontroller/myaction/4/12
But having put this in my bootstrap:
Route::set('fichestandard', '<controller>/<action>/<secteur>/<type>', array('secteur' => '[0-9]+', 'type' => '[0-9]+'))
->defaults(array(
'controller' => 'cee',
'action' => 'fichestandard',
));
And this in action:
public function action_fichestandard($secteur, $type)
Will not someone tell me where I made my mistake?
If you are using Kohana 3.2, controller arguments were removed. You need to use request parameters as described in the manual.
Related
I have this following routing. How to tell /nice-looking-url to load /home/www/html/project/public/static/index.html which is not in the Controller
$route = Http\Literal::factory(array(
'route' => '/nice-looking-url',
'defaults' => array(
'controller' => 'Application\Controller\Nicelookingurl',
'action' => 'index'
),
));
$router->addRoute('nicelookingurl', $route, null);
The simple way to do so is to add this line
return $this->renderScript('insert-your-script-path-here');
as first line in your Nicelookingurl index action
If you want to disable the layout you can also add
$this->_helper->layout()->disableLayout();
I have the following code where I want to find a model using a field called link instead of id. However, it doesn't seem to produce any results. Where could I be getting it wrong? It returns 404
public function actionView($link)
{
$model = News::find()->where(['link'=>$link])->all();
return $this->render('view', [
'model' => $model,
]);
}
NB: in the search model, I have tried adding this:
$query->andFilterWhere([
'id' => $this->id,
'link'=>$this->link,
'category' => $this->category,
'date' => $this->date,
'userid' => $this->userid,
'featured' => $this->featured,
]);
If you want a single model you need one() and not all()
public function actionView($link)
{
$model = News::find()->where(['link'=>$link])->one();
return $this->render('view', [
'model' => $model,
]);
}
With one() method you retrive just a model and in your $model you have the data you need ..
If you sue all() lie you did you retrive a collection of models and for accessing a single model you must set a proper index eg:
$my_model = $model[0];
In UrlManager config you propably have:
'<controller>/<action>/<id:\d+>' => '<controller>/<action>',
It means default rewrite rules use id as param and it has to be digit, so you can't use controller/view/link. Just to be sure, change action name from actionView to actionTest and then call URL controller/test?link=linklink.
Other solution is to use URL like this: controller/view?link=linklink
The problem is in your url
case 1)
http://localhost/projectName/backend/web/controllerName/actionName/username/rushil
Gives output : Not Found (#404)
case 2)
http://localhost/projectName/backend/web/controllerName/actionName/rushil
Gives output : Not Found (#404)
case 3)
http://localhost/projectName/backend/web/controllerName/actionName?username=rushil
this will work.
Solution : check your url and pass link parameter in url as shown in case 3
404 means your action was not found by url manager. link parameter must be in the url (http://yourapp/controller/view?link=something) because you definded it as actionView($link)
Requesting http://yourapp/controller/view will give you 404 error.
For anyone who may encounter such an error, the problem is after using pretty urls, you need to add rules to map the url format.
C:\xampp\htdocs\your_project\frontend\config\main.php under urlManager add the rules like so:
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => 'news/view',
'<controller:\w+>/<link>' => 'news/latestnews', // This is required for $link parameter
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>/<link>' => 'news/latestnews',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
],
I am trying to configure Yii2 url manager in a manner that if a controller name is skipped in url it should call the default controller for action. I have managed to achieve this without action parameter. But got stuck when using parameters in action name.
Here is my route config:
return [
'catalog/category/<alias:[\w-]+>' => 'catalog/default/category',
'catalog/<action:\w+>' => 'catalog/default/<action>',
];
Controller File:
namespace app\modules\catalog\controllers;
use yii\base\Controller;
use app\modules\catalog\models\Categories;
class DefaultController extends Controller
{
public function actionShopbydepartment()
{
$data['categories'] = Categories::findParentSubHierarchy();
return $this->renderPartial('shopbydepartment', $data);
}
public function actionCategory($alias = null)
{
die(var_dump($alias));
$data['category'] = Categories::findCategoryBySlug($alias);
return $this->render('category', $data);
}
}
when I access the following url it loads perfectly.
http://domain.com/index.php/catalog/shopbydepartment
But when i access the below url it called the right function but did not pass the $alias value:
http://domain.com/index.php/catalog/category/appliances
UPDATE:
I have used the following approach for module wise url rules declaration:
https://stackoverflow.com/a/27959286/1232366
Here is what i have in the main config file:
'rules' => [
[
'pattern' => 'admin/<controller:\w+>/<action:[\w-]+>/<id:\d+>',
'route' => 'admin/<controller>/<action>'
],
[
'pattern' => 'admin/<module:\w+>/<controller:\w+>/<action:[\w-]+>/<id:\d+>',
'route' => 'admin/<module>/<controller>/<action>'
],
],
the admin is working fine and this is my first module so rest of the rules are mentioned already
Well just to help other fellows I have retrieve the value of $alias using the following approach:
$alias = \Yii::$app->request->get('alias');
But definitely this is not an accurate answer of the question. I still didn't know what i am doing wrong that i didn't get the value using the approach mentioned in question.
It wirk!
[
'name' => 'lang_country_seller_catalog',
'pattern' => '<lang:\w+>-<country:\w+>/seller/catalog/<module>/<controller>/<action>',
'route' => 'seller/catalog/<module>/<controller>/<action>',
],
[
'name' => 'lang_country_seller_catalog_attributes',
'pattern' => '<lang:\w+>-<country:\w+>/seller/catalog/attributes/<module>',
'route' => 'seller/catalog/attributes/<module>',
],
I'm using CakePHP 3 and I want to paginate my users.
However when I click on the second page, the URL looks like /users?page=2 and I expect : /users/2.
I created this route in routes.php :
$routes->connect('/users/:page', ['controller' => 'users', 'action' => 'index'], ['page' => '[0-9]+']);
And in Users/index.ctp before the "prev" button I put :
<?php
$this->Paginator->options([
'url' => [
'controller' => 'users',
'action' => 'index'
]
]);
?>
Now when I click on page 2 for example, /users/2 opens and I got this error message (RuntimeException) :
Unable to locate an object compatible with paginate.
Did I miss something or where I made a mistake ?
Thanks for your help.
The PaginatorHelper has built in the url format, i.e. to use ?page=n. It will also do sorting such as users?page=2&sort=user_id&direction=asc. Your format of /users/{page} does not handle sorting.
If your REALLY want to stick to /users/{page} you'll have to override PaginatorHelper.
try this
in side your controller with paginator component . It works for me
$this->Paginator->paginate('Users')
for custom urlenter code here
u need to implement index action as
public function index($page = null){
$this->Paginator->settings = ['limit' => 15, 'page' => $page];
$this->set('users', $this->Paginator->paginate('Users'));
}
I can't figure out to generate Url from everywhere i want to, in zend 2
I get action and controller so i try this:
$this->url('myControllerName', array('action' => 'myActionName'));
But this return an object, i just want the full URL string of this route
Somebody can help me to find the proper way?
EDIT : according to Stoyan, maybe i made a mistake on my route. here is the part of my module.config
'router' => array (
'routes' => array (
'indexqvm' => array (
'type' => 'segment',
'options' => array (
'route' => '/Indexqvm[/:action][/:id_event]',
'constraints' => array (
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
'id_event' => '[0-9]+'
),
'defaults' => array (
'controller' => 'Qvm\Controller\Indexqvm',
'action' => 'index'
)
)
),
And my call :
echo $this->url('indexqvm', array('action' => 'list-index'));
the error :
Catchable fatal error: Object of class Zend\Mvc\Controller\Plugin\Url could not be converted to string
Use the echo before calling $this->url(...) (see bellow) and this will display the whole URL.
<?php echo $this->url('route-name', $urlParams, $urlOptions); ?>
Note that the first paramter of url() is the name of the route as specified in your [module]/config/module.config.php file.
See this for more information about ZF2's URL view helper.
EDIT in response to the question edit:
The above section is related to using the URL view helper.
If you want a URL in the controller then you need the URL controller plugin.
<?php $url = $this->url()->fromRoute('route-name', $params, $options); ?>
This is the reference to the ZF2 manual for this controller plugin.
Hope this helps :)
Stoyan
You can use this in the .phtml file
echo $this->url('HelloWorld/default', array('controller'=>'Index', 'action'=>'registration'));
Where HelloWorld/default is the routing and the remaining is the controller and its action and also you can send the others parameter adding just in array as key and value pair.