How to recognize slugified values in yii routing - php

I added this route to the rules array in the main config.
group/<id:\d+>/<name:\w+>' => 'group/index',
public function actionIndex($id, $name)
{
$this->render('index');
}
When i goto the address /group/1/nameofgroup it works great, but when I try to goto the route /group/1/name-of-group I immediately get the error:
Error 404
The system is unable to find the requested action "1".
Does anyone know how to fix this?

The - in name-of-group is not matched by \w, therefore the request is not directed to this route.

I was able to solve this issue, I wish there was a page that showed how to do routing clearly.
'group/<id:\d+>/<name:[\w-]+>' => 'group/index',
was the correct pattern.

Related

Laravel PHPUnit returning 404

For laravel API I have write the test cases. But whenever I am running the test cases it always failed with below error,
1) Tests\Feature\CompanyTest::orgTest
Expected status code 200 but received 404.
Failed asserting that 200 is identical to 404.
After adding the $this->withoutExceptionHandling(); to testcase code it return the below error,
1) Tests\Feature\CompanyTest::orgTest
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST domainname/index.php/api/company
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithExceptionHandling.php:126
/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:415
/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:113
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:507
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:473
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:332
/tests/Feature/CompanyTest.php:21
My Code in Test File is,
public function orgTest()
{
$requestData = ["organizationId" => 10,"offset"=>1,"limit"=>10,"notificationId"=>""];
$response = $this->withoutExceptionHandling();
$response->postJson('/index.php/api/company',$requestData);
$response->assertStatus(200);
}
I have googled the error and tried many solutions but unable to succeed. Anyone please let me know whats the issue.
It is a 404 error, so your webpage is not found :
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST domainname/index.php/api/company
Bad way to go, post to /api/company or with /index.php/api/company but not with "domainname" !
If it is not working, check your route config for that api route. Are your controller and action declared well ?
By default, laravel's routing does not explicitly show that it is a php file rather than it has dedicated endpoints which you decide on the routing file. So index.php shouldn't be a valid route for laravel.
A 404 error status code refers to a Not Found Error. Maybe the api url that you are trying to access does not match the one inside the routes file whether you are using web.php or api.php.
I suggest using php artisan route:list on the base path of the project to properly see that the route you wanted is registered and match it from there.
It seems to me that PHPUnit can't find not only this URL but the whole website. You could check it by request to "/" in your test.
If I'm right, first of all, I'll suggest checking the param APP_URL in your .env.testing. Usually, I set it as APP_URL=http://localhost, and it works for me.
A 404 code means there was nothing found at the specified route. It could be the route you specified index.php/api/company. Try routing to [host:port]/api/company

Laravel subfolder routing error

everyone. Need a bit of help here.
I'm setting up a Laravel project(5.2), inside Laravel's /public folder i have created a /public/blog.
If i access example.com/blog/ - it works fine and returns the blog's home page.
But if i click any article inside the blog, the url is example.com/blog/article_name/ -- This gives an error. It's 404 page/route not found error from Laravel.
Any help would be great, Thank you.
you want to define the route in /routes/web.php
Route::get('/blog/{name}');
I think Route Parameter helps you.
Sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user's ID from the URL. You may do so by defining route parameters:
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
In the same way you can use article name.

Laravel returning a blank page only on certain routes

I'm having an issue where a route is returning a blank page. I am using Homestead as my dev environment and I'm unsure how to debug.
The /storage/logs/laravel ... isn't returning any exceptions when I visit the white page.
web.php (where it's failing):
Route::get('/clinic/register', 'ClinicController#register');
Controller.php:
public function register()
{
return view('clinic.register', ['specialisms' => Specialism::pluck('specialism', 'id')]);
}
Yet when I visit /clinic/register I am shown a blank white page. How can I see why it's failing? Surely a white page will return an exception somewhere?
As you have not provided your entire route setup. This answer is my best guess. See if it helps.
Your issue hint at improper route setup. If you have created a clinic resource then clinic/register route should precede it.
// clinic/register route should come first
Route::get('clinic/register','ClinicController#register');
// followed by rest of the routes which resource will create
Route::resource('clinic','ClinicController');
The reason behind getting a blank pages is because Route::resource will create some route with wildcards.
For e.g. clinic/{clinic} which will map to show method on controller. So when you make a get request to clinic/register it will be mapped to this show method instead of your register method.
One possibility for not getting any errors is your show method does not have any code yet. Hence, a blank response.
To summarize: Order in which you register your routes matters

Yii: How to name a controller action, so the URL included a number

In Yii2, by default, this kind of URL: /foo-bar will be translated to this action:
public function actionFooBar() {
// ...
}
What if I want the same URL to contain a number, like this: /foo-bar-3 ?
I tried actionFooBar3() and actionFooBar_3- no luck, 404 is thrown when I access /foo-bar-3.
Ideas?
UPDATE:
So, the URL threw 404 because there was a typo in it, which I failed to see after too-many hours of work. Don't be a workaholic. Also, I upvote the 3 answers below, as all seem to be valid and might be helpful to others.
I think, you must create new rule for routing, if you wish implement this. In Yii docs said:
Turn the first letter in each word of the action ID into upper case
but number not have upper equivalent. If you define action name as actionFooBar3 - this action is available from url foo-bar3
It is working for me. I Can Access It. Example: https://myYii2.com/foo-bar-3
public function actionFooBar3() {
echo "asd";die;
}
web.php
Check whether enablePrettyUrl is set to true;
'urlManager' => [
'enablePrettyUrl' => true,
],
Error: 404 showing means, you have set some rules for controller actions.
public function actionFooBar3()
{
echo "HI";
exit;
}
And yes please check you have given access rules permission in behaviors method of controller. Sometimes it can be a problem.

Laravel 4 only root route works - other routes return 'Controller method not found'

I've got laravel set up on a domain on a linux host and I have a WAMP local host set up.
The only route that works is the root, when ever I try go to another route such as domain.com/account I get a "Controller method not found." error.
In my routes.php file I have:
Route::controller('','LoginController');
Route::controller('account', 'AccountController');
In my LoginController, I have just two methods. getIndex and postIndex.
After a couple of hours Googling with no results and playing around with the routes file amongst things, still nothing worked.
I tried adding the below route which didn't work either.
Route::any('hello', function(){
return 'hello!';
});
However, I then commented out my Route::controller('','LoginController'); line and the other routes started working!
I then changed it to Route::controller('login','LoginController'); and this and the other routes still worked. I then changed it to Route::any('','LoginController#getIndex'); and the root and other routes still worked. However, doing it this way, when I cliked the login button on my page nothing happened.
So my question really is, is there something wrong with doing Route::controller('','LoginController');? As everything else seems to 'work'
Laravel save an internal collection of registered routes in the $routes member of the Router class. When dispatching a request, a process of picking each element from this collection and test with current request will be executed to find out which route will be handle. This process is affected by the order of your route registering statements.
When testing each route with the current request, the picked route will be compiled and have a regex pattern. This pattern will be use to check with the current URI by the preg_match function as you can see at this line in Laravel source.
When using Route::controller a special route will be add to your routes collection. If your input is Route::controller($uri, $controller) then this special routes will have a regex pattern as ^/$uri/?P<_missing>(.*)$ and it tells Laravel that this request belong to a missing method of the $controller controller class.
In your case, you have set the value of $uri to an empty string which cause the regex of the special route to be ^/?P<_missing>(.*)$ (setting $uri with the string / cause the same effect). Well, this regex will match every URI. So, the internal route looking up process will abort when look to this special route. This is the reason while the exception has been thrown.
You should not use an empty string or the/ string when register with the Route::controller method like the way you did. Instead, use Route::resource or explicit calls (Route::get, Route::post, ...) to handle your top level routes.
I have not tried that, but maybe you could add a "/":
Route::controller('/','LoginController');
Edit
I was able to reproduce the issue and I solved by changing the order of your route lines:
Route::controller('accounts', 'AccountController');
Route::controller('','LoginController');

Categories