Laravel 5 - Router проблем , не мога да взема $id и $slug

C++ JAVA
User avatar
Ticketa
Турист
Турист
Posts: 575
Joined: Mon Feb 27, 2012 1:54 pm
Answers: 24
Location: in /root
Contact:

Laravel 5 - Router проблем , не мога да взема $id и $slug

Post by Ticketa » Wed Nov 18, 2020 11:00 am

Опитвам се, да взема $id и $slug при Laravel 5 , за целта си правя ексиперимент в web.php рутера.



Route::get('/imot/{id}/{title}', function($id, $title){
echo $id . ' - ' . $title;
});

Пиша в браузъра

saita.com/imot/123456/zaglavie

И ми връща:
Page Not found
Sorry, the page you are looking for could not be found.


Страницата не е открита? Не би трябвало ли да ми отвори данните, които съм написал?
by dakata__92 » Fri Nov 20, 2020 7:36 am
dakata__92 wrote:
Wed Nov 18, 2020 12:58 pm
Вместо Closure използвай Controller и вероятно проблема ще ти се реши.

Code: Select all


Route::get('/imot/{id}/{title}', 'ImotController@index');

Пробва ли с това?
Go to full post

User avatar
deam0n
Web-tourist
Web-tourist
Posts: 3036
Joined: Sat Oct 01, 2005 4:33 pm
Answers: 204
Location: Шопландия
Contact:

Re: Laravel 5 - Router проблем , не мога да взема $id и $slug

Post by deam0n » Wed Nov 18, 2020 11:17 am

Това си работи при мен. Някъде другаде ще да е грешката. Я дай целия web.php.

Какво ти връща:

Code: Select all

php artisan route:list
Също така можеш да пробваш да изпълниш и ако е кеширало route-овете.

Code: Select all

php artisan cache:clear
php artisan route:cache

Имаш ли mod_rewrite и htaccess файла?

User avatar
Ticketa
Турист
Турист
Posts: 575
Joined: Mon Feb 27, 2012 1:54 pm
Answers: 24
Location: in /root
Contact:

Re: Laravel 5 - Router проблем , не мога да взема $id и $slug

Post by Ticketa » Wed Nov 18, 2020 11:58 am

Имаш ли mod_rewrite и htaccess файла? - да

В момента системата си работи със уникални слъгове, просто искам да добавя и ID


php artisan route:cache
Route cache cleared!

LogicException : Unable to prepare route [imot/{id}/{title}] for serialization. Uses Closure.

at /vendor/laravel/framework/src/Illuminate/Routing/Route.php:880
876| */
877| public function prepareForSerialization()
878| {
879| if ($this->action['uses'] instanceof Closure) {
> 880| throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
881| }
882|
883| $this->compileRoute();
884|

Exception trace:



web.php

Code: Select all

<?php
Route::get('/imot/{id}/{title}', function($id, $title){
echo $id . ' - ' . $title;
});
// FRONT-END ROUTES
Route::get('/', 'FrontpageController@index')->name('home');

Route::get('/slider', 'FrontpageController@slider')->name('slider.index');

Route::get('/search', 'FrontpageController@search')->name('search');

Route::get('/property', 'PagesController@properties')->name('property');


Route::get('/property/{id}', 'PagesController@propertieshow')->name('property.show');


#Route::get('/property/{id}/{id}', 'PagesController@propertieshow')->name('property.show');

Route::post('/property/message', 'PagesController@messageAgent')->name('property.message');
Route::post('/property/comment/{id}', 'PagesController@propertyComments')->name('property.comment');
Route::post('/property/rating', 'PagesController@propertyRating')->name('property.rating');
Route::get('/property/city/{cityslug}', 'PagesController@propertyCities')->name('property.city');

Route::get('/agents', 'PagesController@agents')->name('agents');
Route::get('/agents/{id}', 'PagesController@agentshow')->name('agents.show');

Route::get('/gallery', 'PagesController@gallery')->name('gallery');

Route::get('/blog', 'PagesController@blog')->name('blog');
Route::get('/blog/{id}', 'PagesController@blogshow')->name('blog.show');
Route::post('/blog/comment/{id}', 'PagesController@blogComments')->name('blog.comment');

Route::get('/blog/categories/{slug}', 'PagesController@blogCategories')->name('blog.categories');
Route::get('/blog/tags/{slug}', 'PagesController@blogTags')->name('blog.tags');
Route::get('/blog/author/{username}', 'PagesController@blogAuthor')->name('blog.author');

Route::get('/contact', 'PagesController@contact')->name('contact');
Route::post('/contact', 'PagesController@messageContact')->name('contact.message');


Auth::routes();

Route::group(['prefix'=>'admin','namespace'=>'Admin','middleware'=>['auth','admin'],'as'=>'admin.'], function(){

    Route::get('/', 'DashboardController@index')->name('dashboard');
    Route::get('dashboard','DashboardController@index')->name('dashboard');
    Route::resource('tags','TagController');
    Route::resource('categories','CategoryController');
    Route::resource('posts','PostController');
    Route::resource('features','FeatureController');
    Route::resource('properties','PropertyController');
    Route::post('properties/gallery/delete','PropertyController@galleryImageDelete')->name('gallery-delete');
   #Route::resource('sliders','\Admin\SliderController');
    Route::resource('sliders','SliderController');
    Route::resource('services','ServiceController');
    Route::resource('testimonials','TestimonialController');

    Route::get('galleries/album','GalleryController@album')->name('album');
    Route::post('galleries/album/store','GalleryController@albumStore')->name('album.store');
    Route::get('galleries/{id}/gallery','GalleryController@albumGallery')->name('album.gallery');
    Route::post('galleries','GalleryController@Gallerystore')->name('galleries.store');


    Route::get('settings', 'DashboardController@settings')->name('settings');
    Route::post('settings', 'DashboardController@settingStore')->name('settings.store');

    Route::get('profile','DashboardController@profile')->name('profile');
    Route::post('profile','DashboardController@profileUpdate')->name('profile.update');

    Route::get('message','DashboardController@message')->name('message');
    Route::get('message/read/{id}','DashboardController@messageRead')->name('message.read');
    Route::get('message/replay/{id}','DashboardController@messageReplay')->name('message.replay');
    Route::post('message/replay','DashboardController@messageSend')->name('message.send');
    Route::post('message/readunread','DashboardController@messageReadUnread')->name('message.readunread');
    Route::delete('message/delete/{id}','DashboardController@messageDelete')->name('messages.destroy');
    Route::post('message/mail', 'DashboardController@contactMail')->name('message.mail');

    Route::get('changepassword','DashboardController@changePassword')->name('changepassword');
    Route::post('changepassword','DashboardController@changePasswordUpdate')->name('changepassword.update');

});

Route::group(['prefix'=>'agent','namespace'=>'Agent','middleware'=>['auth','agent'],'as'=>'agent.'], function(){

    Route::get('dashboard','DashboardController@index')->name('dashboard');
    Route::get('profile','DashboardController@profile')->name('profile');
    Route::post('profile','DashboardController@profileUpdate')->name('profile.update');
    Route::get('changepassword','DashboardController@changePassword')->name('changepassword');
    Route::post('changepassword','DashboardController@changePasswordUpdate')->name('changepassword.update');
    Route::resource('properties','PropertyController');
    Route::post('properties/gallery/delete','PropertyController@galleryImageDelete')->name('gallery-delete');

    Route::get('message','DashboardController@message')->name('message');
    Route::get('message/read/{id}','DashboardController@messageRead')->name('message.read');
    Route::get('message/replay/{id}','DashboardController@messageReplay')->name('message.replay');
    Route::post('message/replay','DashboardController@messageSend')->name('message.send');
    Route::post('message/readunread','DashboardController@messageReadUnread')->name('message.readunread');
    Route::delete('message/delete/{id}','DashboardController@messageDelete')->name('messages.destroy');
    Route::post('message/mail', 'DashboardController@contactMail')->name('message.mail');

});

Route::group(['prefix'=>'user','namespace'=>'User','middleware'=>['auth','user'],'as'=>'user.'], function(){

    Route::get('dashboard','DashboardController@index')->name('dashboard');
    Route::get('profile','DashboardController@profile')->name('profile');
    Route::post('profile','DashboardController@profileUpdate')->name('profile.update');
    Route::get('changepassword','DashboardController@changePassword')->name('changepassword');
    Route::post('changepassword','DashboardController@changePasswordUpdate')->name('changepassword.update');

    Route::get('message','DashboardController@message')->name('message');
    Route::get('message/read/{id}','DashboardController@messageRead')->name('message.read');
    Route::get('message/replay/{id}','DashboardController@messageReplay')->name('message.replay');
    Route::post('message/replay','DashboardController@messageSend')->name('message.send');
    Route::post('message/readunread','DashboardController@messageReadUnread')->name('message.readunread');
    Route::delete('message/delete/{id}','DashboardController@messageDelete')->name('messages.destroy');

});

User avatar
deam0n
Web-tourist
Web-tourist
Posts: 3036
Joined: Sat Oct 01, 2005 4:33 pm
Answers: 204
Location: Шопландия
Contact:

Re: Laravel 5 - Router проблем , не мога да взема $id и $slug

Post by deam0n » Wed Nov 18, 2020 12:37 pm

Не знам. Не виждам нищо нередно в web.php. При мен си работи като тествах на laravel 5.8. Пробвай да го дефинираш към някой контролер.

User avatar
dakata__92
Web-tourist
Web-tourist
Posts: 3351
Joined: Tue Aug 02, 2011 7:24 pm
Answers: 128

Re: Laravel 5 - Router проблем , не мога да взема $id и $slug

Post by dakata__92 » Wed Nov 18, 2020 12:58 pm

Вместо Closure използвай Controller и вероятно проблема ще ти се реши.

Code: Select all


Route::get('/imot/{id}/{title}', 'ImotController@index');


User avatar
anonimen
Web-tourist
Web-tourist
Posts: 1600
Joined: Mon Jun 11, 2012 6:07 pm
Answers: 165
Location: Parse error: unexpected "}" in /home/index.php on line 26

Re: Laravel 5 - Router проблем , не мога да взема $id и $slug

Post by anonimen » Wed Nov 18, 2020 1:21 pm

Хмм, казват, че това е проблем само при кеширането, иначе трябва да си работи…

https://stackoverflow.com/questions/452 ... es-closure

User avatar
Ticketa
Турист
Турист
Posts: 575
Joined: Mon Feb 27, 2012 1:54 pm
Answers: 24
Location: in /root
Contact:

Re: Laravel 5 - Router проблем , не мога да взема $id и $slug

Post by Ticketa » Thu Nov 19, 2020 5:15 pm

Във файла: /app/Providers/RouteServiceProvider.php

Открих следното нещо:

Code: Select all

    public function boot()
    {

        Route::bind('property', function($slug) {
            return Property::where('slug', $slug)->first();
        });

        parent::boot();
    }
Дали проблема не идва от него?

User avatar
dakata__92
Web-tourist
Web-tourist
Posts: 3351
Joined: Tue Aug 02, 2011 7:24 pm
Answers: 128

Re: Laravel 5 - Router проблем , не мога да взема $id и $slug

Post by dakata__92 » Fri Nov 20, 2020 7:36 am

dakata__92 wrote:
Wed Nov 18, 2020 12:58 pm
Вместо Closure използвай Controller и вероятно проблема ще ти се реши.

Code: Select all


Route::get('/imot/{id}/{title}', 'ImotController@index');

Пробва ли с това?

User avatar
Ticketa
Турист
Турист
Posts: 575
Joined: Mon Feb 27, 2012 1:54 pm
Answers: 24
Location: in /root
Contact:

Re: Laravel 5 - Router проблем , не мога да взема $id и $slug

Post by Ticketa » Fri Nov 20, 2020 5:55 pm

Така бачка. Странно. След като изтрих кеша за 4 път проработи. Благодаря момци! :violin:

User avatar
Revelation
Web-tourist
Web-tourist
Posts: 910
Joined: Sun Mar 24, 2013 1:23 pm
Answers: 64

Re: Laravel 5 - Router проблем , не мога да взема $id и $slug

Post by Revelation » Sat Nov 21, 2020 10:40 pm

Въпреки, че си получил отговор, за в бъдеще да знаеш, че Laravel 5 е много общо казано. Във всички 5.х версии има достатъчно промени, които може да са причина за проблема, така че ти предлагам за в бъдещи теми относно Ларавел да споделяш пълната версия, която използваш. :wink:

Post Reply