Laravel using of resource - laravel

I'm new to Laravel and I'm working around with it.
I have a folder in views called 'cp'. in cp folder I can add/remove new slider through index.blade.php and also add new products through product.blade.php. I want to update my products too but when I submit the form It goes to the wrong route ( blog.dev/product/6 ).
I also attached 4 screenshots that shows the pages.
Could you please help me with that.
This is web.php:
Route::get('/','PagesController#index');
Route::get('درباره-ما', 'PagesController#about');
Route::get('تماس-با-ما', 'PagesController#contact');
Route::get('غذای-سگ', 'PagesController#dogs');
Route::get('/cp/product', 'ProductController#product');
Route::get('/cp/product/{id}/edit/', 'ProductController#edit');
Route::resource('product','ProductController');
Route::resource('cp', 'PostsController');
Auth::routes();
screenshot1: https://cdn.pbrd.co/images/GSFDbPj.jpg
screenshot2: https://cdn.pbrd.co/images/GSFDNRq.jpg
screenshot3: https://cdn.pbrd.co/images/GSFEi1a.jpg
screenshot4: https://cdn.pbrd.co/images/GSFCI6F.jpg

Laravel read the route from the top and stop at the first match, so put the more restrictive route at the top
Try something like this : (unfortunately I can't check the non-latin alaphabet, maybe you can also comment them if you still have issue - just to be sure it's not a cause too)
Route::get('/','PagesController#index');
Route::get('درباره-ما', 'PagesController#about');
Route::get('تماس-با-ما', 'PagesController#contact');
Route::get('غذای-سگ', 'PagesController#dogs');
Route::get('/cp/product/{id}/edit/', 'ProductController#edit');
Route::get('/cp/product', 'ProductController#product');
Route::resource('product','ProductController');
Route::resource('cp', 'PostsController');
Auth::routes();
Can you tell me if it's working ?

update method available only PUT|PATCH method
change your request url
/product/6?_method=PUT
now you can use POST method for update

Related

Location Code in Url - Laravel

I'm creating a directory for multiple locations for example, USA, UK and Canada.
I want something like this www.example.com/US and www.example.com/UK, www.example.com/CA
When someone visits www.example.com/US i want to list all listings under USA location. Similarly for other locations. For this i have a single database and multiple laravel installation for each country. (I'm new to laravel so do not know how to do this. I'm trying something on my own with my knowledge.)
I have added a prefix to the routes so that it adds the country code in the URL
Route::group(['prefix' => 'USA'], function(){
Route::get('/', 'Frontend\PagesController#index')->name('welcome');
});
When i visit www.example.com it shows page not found. However when i visit www.example.com/USA it works all fine.
I have two questions here.
1. Is this the right way to do it? Or are there any other methods with single installation like locales are handled.
2. How to use the default url as www.example.com/country-code-of-user (Auto Detect). I made a dropdown to change the country.
The first question is a general question which i wanted to check here with senior programmers. The later question is about the error im receiving "Page Not Found" which i want to solve.
Any help would be appreciated. Thanks
If you have you got user country then you can redirect user to the proper url using some code like as :
Route::get('/', function () {
return redirect('/USA');
});
// OR you can use any controller method to get conditional country redirection
Route::get('/', 'User#redirectTo')
//...with method
public redirectTo()
{
// if some condition
return redirect('/USA');
// else redirect to CA, UK and so on..
}

Laravel: Creating route resource for sub directory

I am new to laravel and creating a spare parts maintenance app.
I created a route resource for spare parts using :
Route::resource('/parts' , 'SparePartsController');
This works fine.
Later I wanted to also create another route resource for spare parts categories. So I created the controller and used:
Route::resource('/parts/categories' , 'SpCategoriesController');
But this second resource wont work. When i go to www.myapp.com/parts/categories , I get a blank page. Any idea whats wrong?
Try to group the routes with a prefix for example:
Route::group(['prefix' => 'parts'], function(){
Route::get('/', 'SparePartsController');
Route::get('/categories', 'SpCategoriesController');
});
This will route all traffic from /parts to the SparePartsController and /parts/categories will call the SpCategoriesController
Look at the documentation for more information:
https://laravel.com/docs/5.3/routing#route-groups
If you want to call a specific function of the Controller just write:
Route::get('/', 'SparePartsController#functionName')
The SpCategoriesController resource route won't work because SparePartsController resource route is taking precedence over it.
To fix that, place your routes in this order:
Route::resource('/parts/categories' , 'SpCategoriesController');
Route::resource('/parts' , 'SparePartsController')
Ref: https://laravel.com/docs/5.3/controllers#restful-supplementing-resource-controllers

laravel routing based on convention

I'm trying to setup a simple routing system based on convention.
My app will have this structure
Http
--Controllers
----Admin
------User.php
----Books
------Add.php
----etc...
I want to be able to add new Folders and controllers without adding routes manually to the web.php file.
For example I want the route to respond to /Admin/User URL with User.php controller.
I'm trying something like this, but I don't understand how to write the internal router...
Route::any('/{module}/{action?}', function($module, $action = 'index') {
Route::get('*',$module.'\'.$action.'#index' );
});
It seems that Rout:get('*'... never matches.
PS the controller namespace is correct and I reloaded with composer.
The controller works if called harcoded.
I tried also to escape '\'
$r=$module.'\\'.$action.'\\'.$action.'Ctl#index';
Route::get('/',$r );
But no result. The route is intercepted but nothing i served
It seems I came up with this
Route::get('/{module}/{action}', function($module,$action) {
return App::make('\App\Http\Controllers\\'
.$module.'\\'.$action)->callAction('index', []);
});
Any other better way?

Subfolder routing in laravel 5

I am having trouble routing with controllers in subfolders. I have tried the solution proposed in Laravel Controller Subfolder routing, but I can't get it to work.
Folder structure
HTTP
Controllers
Admin
AdminControllers
User
UserControllers
BaseController
Admincontrollers are defined in the 'App\HTTP\Controllers\Admin' namespace
Routes file
Route::group(['middleware'=> 'admin','prefix' => 'admin'], function() {
Route::get('home', 'AdminHomeController#index');
Route::get('home', 'Admin\AdminHomeController#index');
Route::resource('events', 'AdminEventController');
Route::resource('events', 'Admin\AdminEventController');
Route::get('myevents', 'AdminEventController#myevents');
Route::get('myevents', 'Admin\AdminEventController#myevents');
Route::resource('groups', 'AdminGroupController');
Route::resource('users', 'AdminUserController');
});
This does seem weird, but it is the only way to keep it working right now.
If I delete
Route::get('myevents', 'Admin\AdminEventController#myevents');
//errormessage Class App\Http\Controllers\AdminEventController does not exist
If I delete
Route::get('myevents', 'AdminEventController#myevents');
//errormessage Action App\Http\Controllers\AdminEventController#myevents not defined.
If I put the controllers in the controller namespace (not the admin one)
I still get
//errormessage Class App\Http\Controllers\AdminEventController does not exist
When the only route added is
Route::resource('events', 'AdminEventController');
The problem were the calls in the views:
changing
<td>{!!Html::linkAction('AdminEventController#show', $event->name, $event->slug)!!}</td>
to
<td>{!!Html::linkAction('Admin\AdminEventController#show', $event->name, $event->slug)!!}</td>
fixed it.
The Laravel 5 solution in Laravel Controller Subfolder routing is correct. The problem was not in the routes file or controllers.
Yes If your application gets bigger like this, it makes sense to structure Controllers with sub-folders. But it takes a little more effort than just moving the files here and there. Let me explain the structure.
For example, we want to have a sub-folder app/Http/Controllers/Admin and then inside of it we have our AdminController.php, that’s fine. What we need to do inside of the file itself:
Correct namespace – specify the inner folder:
namespace App\Http\Controllers\Admin;
Use Controller – from your inner-namespace Laravel won’t “understand” extends Controller, so you need to add this:
use App\Http\Controllers\Controller;
Routes – specify full path
This wouldn’t work anymore:
Route::get('admin', 'AdminController#index');
This is the correct way:
Route::get('admin', 'Admin\AdminController#index');
And that’s it – now you can use your controller from sub-folder.
Reference (Tested):
http://laraveldaily.com/moving-controllers-to-sub-folders-in-a-correct-way/
By: Povilas Korop

Form open to controller method - "Unknown action"

New to Laravel 4. I've created a form within a blade template and I'm following the snippet from which says that you can point a forms action to a controller method by using 'Form::open(array('action' => 'Controller#method'))'. I've created a new controller called UsersController with artisan and have created a new method within the controller named userLogin(). When I point to that method when opening a form I get an "InvalidArgumentException, Unknown action" error. If I adjust the open action to point to UsersController#index, all is well. I've run composer dump-autoload, but the issue remains.
snippet of login.blade.php:
{{ Form::open(array('action' => 'UsersController#userLogin')) }}
snippet of UsersController.php:
public function userLogin()
{
//
}
Can anyone tell me if I'm missing something?
Thanks all. Adding the following to routes.php resolved the issue:
Route::post('login', 'UsersController#userLogin');
Looks like Laravel isn't registering the action you've added, likely because you're missing a route. Try adding something like this to app/routes.php:
Route::post('user/login', 'UsersController#userLogin');
After adding the route to your routes.php, did you also change Form::open()? If not, you can just have your Form post to /login or /user/login.
Also, just because I'm a bit of a stickler for these sort of things, it's common practise to have controllers and models as singular, so UsersController would be UserController, and since the login function is within User(s)Controller, it doesn't need the user prefix. May help your code be more readable :)
Now in laravel 4 you can use this :
Route::post('/signup', array('before' => 'csrf', 'uses' => 'UsersController#userLogin'));

Resources